TAGS :Viewed: 9 - Published at: a few seconds ago

[ Memory stacks with no good reason? ]

Hello im building a c# web application, which needs to run alot of checks on every page request. Now i needed to cache product information and attributes of those products so that is stacking up memory (obviously). But when i reload a page with alot of products it makes some Sql requests with those product names into strings. I have noticed strings take up alot of memory but when im ready with those strings ( which are created locally ) the memory used for those strings keeps on building up till a certain (random) point where it drops back down. Then it starts to build up again.

I have used .NET Memory Profiler to analyze the memory use and it states that System.String even uses more then the cached products and attributes.

I have optimized some strings building up alot of memory in this situation:

Before optimization

string selectSql = "";
for(var i = 0; i < products.count;i++)
{
    selectSql += "'"+products[i].Name+"', ";
}

After optimization

List<string> listSelect = new List<string>();
for(var i = 0; i < products.count;i++)
{
    listSelect.Add(products[i].Name);
}
string joined = "'" + String.Join("', '", listSelect) + "'";

But as far as i can see im not using alot of those situations that would build up that much memory, how can i clear that memory?

I cant use:

GC.Collect();

Or atleast alot of people advice against this method because it would only give a hint to the garbage collection, and in my case it wont clean up the memory. I have checked it with

GC.GetTotalMemory(true);

It is the same after a Collect statement.

Where am i going wrong what do i miss or what can i do to make sure those strings wont be in my memory for long?

Thanks in advance for your time.

After some request for more code this is what i finnaly used as "query builder", listSelect is the List from previous mentioning.

StringBuilder sb = new StringBuilder();
sb.Append("SELECT pr.* FROM products pr");
sb.Append("HAVING pr.Name IN(" + "'" + String.Join("', '", listSelect) + "'" + ") ");
sb.Append("ORDER BY pr.`Prijs` ASC LIMIT 1");
db.Sql = sb.ToString();

But when i do db.Sql = sb.ToString(); Isnt that using up as much memory as just a string made? Or is the StringBuilder still using that in a good way?

Answer 1


You are observing the garbage collector doing its job. When it needs to reclaim memory, it does so.

Unless you are suffering performance issues, then memory is there to be used and your application is working just fine. You don't need to try and fix something that isn't broken.