Reputation: 215
How does the garbage collector know the objects and variables are out of scope so they can be collected by garbage collector?
Upvotes: 4
Views: 2441
Reputation: 1007
The garbage collector (GC) is a part of the .NET framework which is initialized by the common language run-time (CLR) to manage the allocation and release of memory in an application.
Types of garbage collector
The garbage collector can work in a wide variety of scenarios. The CLR provides the following types of garbage collection:
Concurrent garbage collection Concurrent garbage collection allows user threads to run for the most of generation 2 garbage collection. As long as there is still free space in the managed heap for new allocations, user threads are allowed to run and create new objects.
Non-concurrent garbage collection Non-concurrent garbage collection suspends all non-garbage-collection threads for the full duration of the garbage collection, effectively pausing the application for that time.
Background garbage collection Background garbage collection is the replacement for concurrent garbage collection. It is similar to concurrent garbage collection but allows generation 0 and generation 1 garbage collection to interrupt an ongoing generation 2 garbage collection and temporarily block program execution. After generation 0 and generation 1 garbage collection is completed, both the program execution as well as the generation 2 garbage collection, continues. This even further shortens the time the program execution is paused because of garbage collection.
Memory and managed heap Once the garbage collector is initialized by the common language runtime (CLR), it allocates a segment of memory called managed heap which opposed to a native heap in the operating system, to store and manage objects.
For each managed process, the garbage collector reserve a segment of memory in the managed heap using the Windows VirtualAlloc function. (It uses the Windows VirtualFree function to release segments back to the OS). All threads in the process allocate memory for objects on the same heap.
One important thing to consider is that the size of each allocated segment is implementation-specific and it's subject to change at any time.
Generations
The objects stores into the managed heap are organized into generations according their age. Garbage collection primarily occurs with the reclamation of short-lived objects that typically occupy only a small part of the heap.
When the garbage collector detects that the survival rate is high in a generation, it increases the threshold of allocations for that generation.
Garbage Collector phases
The collection is triggered by one of the following conditions:
The system has low physical memory. This is detected by either the low memory notification from the OS or low memory as indicated by the host.
The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. 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.
At this point the garbage collector passes throw the following phases:
Marking Phase: The GC create a list of all the live objects (all of
the objects that are not on the list are potentially deleted) by
using the following information the to determine whether objects are
live or not:
Relocating Phase: The references of all the objects that were on the list of all the live objects are updated in the relocating phase so that they point to the new location where the objects will be relocated to in the compacting phase. Compacting Phase: 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. The heap gets compacted in the compacting phase as the space occupied by the dead objects is released and the live objects remaining are moved. All the live objects that remain after the garbage collection are moved towards the older end of the heap memory in their original order
Resources
Official documentation: https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals
Upvotes: 0
Reputation: 60997
No discussion of garbage collection in .NET would be complete without referring to Raymond Chen's excellent series of blog posts:
Here's a quote from the first article in the series:
When you ask somebody what garbage collection is, the answer you get is probably going to be something along the lines of "Garbage collection is when the operating environment automatically reclaims memory that is no longer being used by the program. It does this by tracing memory starting from roots to identify which objects are accessible."
This description confuses the mechanism with the goal. It's like saying the job of a firefighter is "driving a red truck and spraying water." That's a description of what a firefighter does, but it misses the point of the job (namely, putting out fires and, more generally, fire safety).
And here are a few interesting points that he demonstrates:
A correctly-written program cannot assume that finalizers will ever run.
An object in a block of code can become eligible for collection during execution of a function it called.
A parameter to a method can become eligible for collection while the method is still executing.
A weird real-world analogy: The garbage collector can collect your diving board as soon as the diver touches it for the last time—even if the diver is still in the air!
and, most succinctly:
Don't think of GC as tracing roots. Think of GC as removing things you aren't using any more.
Upvotes: 3
Reputation: 12852
Please go through http://msdn.microsoft.com/en-us/magazine/bb985010.aspx. As it says
The garbage collector checks to see if there are any objects in the heap that are no longer being used by the application. If such objects exist, then the memory used by these objects can be reclaimed.
Upvotes: -4
Reputation: 1550
In short: Every application has a set of roots. Roots identify storage locations, which refer to objects on the managed heap or to objects that are set to null.
When the garbage collector starts running, it makes the assumption that all objects in the heap are garbage.
The garbage collector starts walking the roots and building a graph of all objects reachable from the roots.
All objects not reachable are removed (memory is freed)
This is taken from http://msdn.microsoft.com/en-us/magazine/bb985010.aspx - good article about the garbage collection. The "interesting" part for you is "The Garbage Collection Algorithm". It is not a very long section
Upvotes: 7