I have been trying to fix memory leaks in my application for quite a while. Few months back, I noticed that in my application, more than 95% of the objects get promoted to Gen2; and I have some very basic questions which I have not been able to find in books. I hope you guys can help me:
- Does having so many Gen2 objects a bad news? I get around 77,000 Gen2 objects, 100 Gen1 objects and 10 Gen0 objects in WinDbg at every snapshot I take.
- How can I find out the reason so many objects are getting promoted? Is there a good tool? I have been using WinDbg/SOS mostly and I haven’t had much luck
Thank you in advance.
This is not the bad news. It could mean that GC.Collect has just run, or it is run pretty often, and you have plenty of free memory – therefore objects in Gen2 are not collected. Here is some good explanation from another StackOverflow question,
Garbage Collection not happening even when needed:
To your second question:
Windbg and SOS is good 🙂 In this situation I would suggest to add psscor2 (or psscor4, if .NET 4.0 is used) You need to use psscor to dump object of specific generation easily.
Here are official links to download psscor2 and psscor4 dlls. Put appropriate versions of them to the folder where Windbg is installed. x86 versions of dlls to x86 folder (C:\Program Files (x86)\Debugging Tools for Windows), x64 versions of dlls to x64 folder (C:\Program Files\Debugging Tools for Windows (x64)).
Then you will be able to run next command:
SOS doesn’t have easy command to dump objects only of specific generation, when psscor does.
Using this command, you may create constructions like
or
to find out main types of the objects in gen 2, and which methods reference them.
As you said, that your main idea is to find memory leaks, you should use more general approach, and analyze not only generation. Here are my posts on analyzing high-memory-usage issues:
managed memory:
http://kate-butenko.blogspot.com/2012/06/investigating-memory-issues-high-memory.html
unmanaged memory:
http://kate-butenko.blogspot.com/2012/07/investigating-issues-with-unmanaged.html
http://www.codeproject.com/Articles/31382/Memory-Leak-Detection-Using-Windbg
Please leave your questions if you are still stuck on some step.