If you in the managed program execution world, you cannot avoid the word GC even though everybody says its managed automatically and there is no need to worry about memory management during coding. Ideally the garbage collection ie memory management should be done intelligently by the managed runtime environment otherwise there is no need to adopt that run-time. But the truth is opposite. Earlier when we had managed the memory ourselves, it was easy as we were deleting the objects .But now we need to depend and learn the working of another software piece written by somebody else to effectively manage memory of our application and that is called GC ie the GarbageCollector.Not a joke. If you don't know the working of GC you cannot survive in the managed world .At least it will be a bottle neck in your job interview.
This post is not intended to explain about the GC in paragraphs. This helps to refresh the concepts about GC by just pointing to already written articles. This means you need to have prior knowledge about GC.
What .Net GC does?
- Collects unused objects and frees memory
- Compacts / Defragment the memory of Gen 0,1,2 after clearing the speace used by the objects.
- There is LOH (Large Object Heap) which contains large objects .Normally size > 85KB. This space is not defragmented by GC. Why there is a LOH is because the object moving process is heavy and may impact the performance.
- LOH objects are marked as deleted by GC and GC keeps track of free space in LOH to perform allocation for the next time.
How GC works
- Use generational algorithm.
- Start by assuming that all the objects are garbage except the application roots.
- Application root
- Any static variable is application root
- Any local variable or method params of function which is executing when the GC starts.
- Any object in heap which is pointed by any CPU register.
- Any object whose pointer is present in freachable queue or passed to COM+ via interop.
- It creates memory map by traversing the objects starting from application roots.
- If the object is in use it promotes the object from its current generation to higher generation.
- Generation is simply an area in the memory heap. There are 3 Generations
- Gen 0
- Contains all the newly created objects.
- Gen 1
- Contains objects which are promoted by GC from gen 0 .ie the objects which are still in use.
- Gen 2
- Contains objects which are promoted from Gen 2 .Mainly old objects.
- Once the collection completes GC performs a compaction in the generation.
- After compaction it moves the next object pointer to the end of allocated objects so that next time the new keyword can execute faster.
- Collecting unwanted objects from these 3 generations is time consuming.So it can perform partial collections say collect only Gen 0.
- Once the object seems Garbage to GC it moves the object pointer from Finalization queue to the freachable queue in case the object pointer is present in the finalization queue.
- The entry to the finalization queue is put at the time of object allocation (new keyword) if the Finalize method is overriden
- When any object pointer is available in the f-reachable queue the finalization thread starts and calls the Finalize method on each object. At this time the object becomes non-garbage.
- Next time when GC finds the finalized objects as garbage it simply removed the objects.
- This is a mechanism to object to cleanup themselvs
- Similar to destructor and now C# allows us to write the Finalize in the destructor syntax only.
- This destructor method will be converted to Finalize method during compilation which means there is no real destructor in C# .Net.
- GC calls this Finalize method after marking the object as garbage.
When GC starts collecting garbage?
- It is NOT based on time interval.
- When somebody calls the GC.Collect
- When there is not enough physical memory
- When a generation (heap) is full so that it cannot allocate new objects or move survived objects to next generation.
- Use the perfmon and counters
- Set breakpoint in Windbg on GC starts
- Use ETW tracing listeners on GC
- Write a profiler and monitor the method ICorProfilerCallback2::GarbageCollectionStarted
- Garbage Collection MSDN