'High RAM usage, calling the same endpoint multiple times

I made a controller just to test the stability and velocity of my .net core server. When calling this endpoint with n = 10, the RAM usage goes up to 500mb, which I don't mind, but calling the same endpoint multiple times it ramps up to 3Gb. It is normal? I'm doing something wrong? Can I lower this usage?

    [ApiController]
    [Route("[controller]")]
    public class DebugController : Controller
    {       
        [HttpGet]
        public  IActionResult Index(int n)
        {
          
                long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                
                var T = new DataSet();
                if (n <= 0) { n = 1; }
                else if (n > 10) { n = 10; }

                for (int i = 0; i < n; i++)
                {
                    var table = new DataTable("table" + i);
                    table.Columns.Add("test");
                    for (int j = 0; j < 500_000; j++)
                    {
                        table.Rows.Add("");
                    }
                    T.Tables.Add(table);
                }


                return Ok(new test(now, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), T));
            
        }
             

        public struct test:IDisposable
        {
            public test(long i, long f, DataSet d)
            {
                ini = i;
                fi = f;
                data = d;
            }

            public long ini;
            public long fi;
            public DataSet data;

            public void Dispose()
            {
                data.Dispose();
            }
        }
    }


Solution 1:[1]

You are calling n times. This means each time you call the part below you add a row to your table. This results in saving your structure (your table data) in memory.

for (int j = 0; j < 500_000; j++)
{
    table.Rows.Add("");
}

Iterating that part multiple time increase the amount of rows and therefore you need more storage in memory. Memory should be cleared up after some time by the Garbage Collector automaticlly, since C# is a managed language. Seems to me to be normal to have such a high amount of RAM usage.

There a other ways to handle such situatuion like doing by lazy loading. This may reduce the RAM usage. However I can not tell you if using other data structs like IEnumerable<T> are fitting to your context. Working with IEnumerable<T> without calling .ToList() or .ToArray() will not consume memory until the data is needed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dominik