views:

74

answers:

3

I know it is not possible to know when a GC occurs, but there are factors that will tell you how often/when it may occur. What factors are these? One is how many objects are created, etc.

+1  A: 

Garbage collection occurs when one of the following conditions is true:

The system has low physical memory.

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.

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.

tsinik
+4  A: 

The short answer is that the following events trigger a collection cycle:

Allocation exceeds Gen0 threshold

Collection for a specific generation occurs when the memory threshold for that generation is hit. In the implementation of .NET Version 1.0, the initial thresholds for generations 0, 1, and 2 are 256 kilobytes (KB), 2 megabytes (MB), and 10 MB, respectively. Note that the GC can adjust these thresholds dynamically based on an application's patterns of allocation. Objects larger than 85 KB are automatically placed in the large object heap.

System.GC.Collect() is called

Allocations only happen in Gen0. After each GC, Gen0 is empty. New allocations will fill up Gen0 and the next GC will happen, and so on. The problem with calling GC.Collect() manually, is that you can end up with it being called more often than you predicted (due to the CLR calling it as well) and performance goes down because you are triggering GC cycles ahead of their schedule.

System is in a low memory situation

This is affected by other processes on the system which means you really have no control over it other than ensuring that you clean up resources correctly in your processes and components.

Scott Dorman
These were the factors I saw on another site and forgot. Thanks.
dotnetdev
A: 

For the desktop version of C# there are a few factors:

  • Amount of memory allocation (Every time 1 MB is allocated a GC level 1 is triggered)
  • System memory pressure (if the amount of physical memory gets low, GC may be triggered)
  • Virtual Memory commits (if the system want's to commit memory pages, then a GC may be triggered prior to the operation)
  • Programmatic triggering from code.
  • Other heuristics may be used by GC to trigger collections.

In case you're interested when level 3 collections are about to happen in .Net framework 3.0 and above there are GC notifications that you can subscribe to, to be notified before they happen: RegisterForFullGCNotification

Pop Catalin
I believe that you can only use `RegistorForFullGCNotification` if you are using Server GC not Workstation GC, which is the default for desktop applications.
Scott Dorman
@Scott Dorman, you can register for any type of GC used.
Pop Catalin
Sorry...check this page for more details: http://msdn.microsoft.com/en-us/library/cc713687.aspx `This feature is available only when concurrent garbage collection is disabled. By default, concurrent garbage collection is enabled unless you are running in a hosted environment and the host changed the configuration for you.`
Scott Dorman
@Scott Dorman, noted. Also server GC is default for ASP.Net, which still makes it useful.
Pop Catalin
Yes, Server GC is the default for ASP.NET since IIS is actually hosting the .NET runtime which is running your web applications.
Scott Dorman