Uncategorized

How much garbage to you still have on the managed heap?

When you’re focused on a specific problem, you’re sunk into it and you’re at a dead end, you have to take a step back to see the whole picture and breath again before being able to continue; often discussing with a colleague whom still has a “fresh mind” and doesn’t know anything about the specific problem and the troubleshooting you’ve already done can give nice suggestions and tips about next steps and a different approach to the problem. This is what happened a couple of days ago with Doug while we were discussing a memory leak issue I’m currently working on.

This seems to be a complex and a bit confusing one (I do not want to dig into the details for post, I’ll likely talk again in details when the problem will be resolved 🤓); anyway a question that should always come to mind dealing with memory leaks is: how much of the memory I see still allocated in a dump is really valid and how much is garbage which our friendly Garbage Collector will remove next time it will come into the game?

Well… a nice solution is to have a very simple “service” page to trigger a full Garbage Collection when we need it (this is only for troubleshooting, do not call it too often if you do not want bad performance issues because the GC will be continuously triggered!). The page can be as simple as the following 4 lines of code, for example in the Page_Load event or in a Button_Click handler:

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Response.Write("Full collection run on " + DateTime.Now.ToString());

You can run it when the memory utilization is high and when the page returns you can then capture a hang dump with your favorite tool (see here if you are not an expert and want to do it yourself); at that point you can be sure that what will be left on the managed heap will be valid objects, still rooted somewhere in memory and not garbage. If you repeat the steps 3-4 times when the memory will be grown of another 100-120 Mb you’ll finally have a series of 4 dumps taken at different times to compare and play with (or ready to be sent to CSS ?).

Carlo

Quote of the day:

The effort to understand the universe is one of the very few things that lifts human life a little above the level of farce, and gives it some of the grace of tragedy. – Steven Weinberg

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.