Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unusual Garbage Collection Patterns

I'm seeing some unusual garbage collection patterns under .net 4.0 that I can't explain, any advice would be appreciated.

At various times in the running of my program the G2 collection count begins to increase and GC % time goes to near 100%. This continues for some time before stopping (please see Performance Monitor screen grab below). During this time a PerfView trace of the CLR GC ETW events shows that a number of long running G2 collections are being triggered back to back with reason 'AllocSmall'.

Can anyone shed some light on:

  1. Preciesly what causes an 'AllocSmall' event
  2. Why these are directly triggering G2 collection
  3. Why these G2 collections are occuring back to back

LowMemory does not appear to be the problem as the GC start events do not carry this as their reason code (http://msdn.microsoft.com/en-us/library/ff356162.aspx). We have also seen similar situations in which G0 collections occur back to back.

Edit: It's been suggested that the trigger could be some threshold being exceeded. Given there is still available memory I would expect the G2 heap to expand at this point rather than have the GC thrash trying to collect.

Edit: The pictures posted below are much higher resolution than StackOverflow displays - just open the image link directly in a browser tab.

Performance Monitor TracePerfView Event Trace

like image 752
Simon Walker Avatar asked Feb 08 '12 16:02

Simon Walker


1 Answers

Reasons Garbage Collect Occurs:

  1. The system has low physical memory.

  2. The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This means that a threshold of acceptable memory usage has been exceeded on the managed heap. This threshold is continuously adjusted as the process runs.

  3. The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

Looks like #2 is the case, that is you have 'surpassed an acceptable threshold'. +1 for Microsoft vagueness.

like image 78
Abdul Hfuda Avatar answered Oct 11 '22 14:10

Abdul Hfuda