Do you redefine any finalizers? When the finalizer does not return (maybe you wait at any time for another thread), the application may freeze. GC.Collect() calls the finalizer, but it never returns, since it expects the finalizer to return. This could cause a memory leak because the GC cannot complete garbage collection.
Decision
You should be able to debug it when the GC collects and you press "pause", it should show the stack trace where it hangs. If everything goes wrong, maybe you need to check the disassembly where your application hangs.
GetTotalMemory (true)
This method forces the garbage collection, so it is the same as GC.Collect() .
Update
Let's take a look at GetTotalMemory:
public static long GetTotalMemory(bool forceFullCollection) { long totalMemory = GC.GetTotalMemory(); if (!forceFullCollection) { return totalMemory; } int remainingSteps = 20; long lastMemory = totalMemory; float num4; do { GC.WaitForPendingFinalizers(); GC.Collect(); totalMemory = lastMemory; lastMemory = GC.GetTotalMemory(); num4 = (float)(lastMemory - totalMemory) / (float)totalMemory; } while (remainingSteps-- > 0 && (-0.05 >= (double)num4 || (double)num4 >= 0.05)); return lastMemory; }
As you can see, GetTotalMemory expects finalizers, so it should be a finalizer, a hovering application. GC.Collect() is called when finalizers are finished and will never be called because they never pass a string before.
At any point in your application (unsafe code? Another thread?) There is something that allocates a lot of memory, while the GC never has the ability to clear existing memory.
Renouncement
This is a pure assumption, I donβt know if the GC handles such situations, but I think that it cannot break the finalizer, because it can take a lot of time, and the GC does not know if it just hangs or still finishes.
source share