Skip to content

Paultje52/aspnet-string-array-memory-issue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ASP.net string array memory issue

This is a reproduction repository of a memory issue when returning large string lists in an ApiController.

The problem

In a controller I'm creating a list, adding 1.000.000 strings to it, and then returning it. When navigating to the web page, I'm getting a JSON array with a million strings. This code is located in WebApplication/Controllers/ExampleController.cs.

[HttpGet]
[OutputCache(Duration = 0, NoStore = true)]
public IEnumerable<string> Memory()
{
	List<string> list = new();

	for (var i = 0; i < 1_000_000; i++)
	{
		list.Add(i.ToString());
	}

	return list;
}

The garbage collector should free the memory when I'm done with the request. However, the memory keeps climbing (see screenshot).
image

Expected behaviour

When writing a simple console application, the memory falls back to a level you can expect (see screenshot). This code is located in ConsoleApp/Program.cs.
image

while (true)
{
    GC.Collect();
    GC.WaitForPendingFinalizers();
    Console.ReadLine();

    var list = GetList();
    Console.WriteLine(list.Count);
}

List<string> GetList()
{
    List<string> list = new();
    for (int i = 0; i < 1_000_000; i++)
    {
        list.Add(i.ToString());
    }

    return list;
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages