How reproduce DotMemory's Force GC button's behaviour on code with c#?
When I'm profiling my application with DotMemory I can see that the normal GC.Collect() I added in my code doesn't free memory like the DotMemory Force GC button.
I want to reproduce exactly what happens when I click on the Force GC button, how can I do?
Please sign in to leave a comment.
"Force GC" button calls GC from a native code.
When you call GC.Collect() method from your code, it performs the next steps:
GC.Collect() release only a managed objects. Besides, CLR has several different GC strategies which are applied depending on the situation to minimize delay which caused GC and we can't affect it.
Native memory which used by managed objects will never been released if these objects don't have Finalize method or this method is implemented incorrectly.
We can suggest you to call WaitForPendingFinalizers method with GC.Collect and repeat it several times:
for (int i = 0; i < 4; i++)
{
GC.Collect(2, GCCollectionMode.Forced, true);
GC.WaitForPendingFinalizers();
}
It can show the better result but we can't guarantee that this code will lead to the same results as Full GC called from native code.
GC.Collect method: https://msdn.microsoft.com/en-us/library/hh138633(v=vs.110).aspx