Tuesday, November 22, 2011

What Happens During a Garbage Collection?

A garbage collection has the following phases:


1.A marking phase that finds and creates a list of all live objects.

2.A relocating phase that updates the references to the objects that will be compacted.

3.A compacting phase that reclaims the space occupied by the dead objects and compacts the surviving objects. The compacting phase moves objects that have survived a garbage collection toward the older end of the segment.

Because generation 2 collections can occupy multiple segments, objects that are promoted into generation 2 can be moved into an older segment. Both generation 1 and generation 2 survivors can be moved to a different segment, because they are promoted to generation 2.

The large object heap is not compacted, because this would increase memory usage over an unacceptable length of time.

The garbage collector uses the following information to determine whether objects are live:
Stack roots. Stack variables provided by the just-in-time (JIT) compiler and stack walker.

Garbage collection handles. Handles that point to managed objects and that can be allocated by user code or by the common language runtime.

Static data. Static objects in application domains that could be referencing other objects. Each application domain keeps track of its static objects.

Before a garbage collection starts, all managed threads are suspended except for the thread that triggered the garbage collection.

No comments:

Post a Comment