This document provides an overview of garbage collection including:
- Key memory concepts like physical memory, virtual memory, and address space
- Common GC algorithms like reference counting, mark and sweep, and copying collectors
- Advantages of automatic memory management but also performance impacts
- .NET GC implementation with managed heap and how objects are traced and collected
- Best practices like implementing dispose pattern and avoiding calling GC.Collect
Powerful Google developer tools for immediate impact! (2023-24 C)
Chronicles Of Garbage Collection (GC)
1. THE CHRONICLES OF GARBAGE COLLECTION
BY
Rupreet Singh Gujral (rupreetg@outlook.com)
Product Development / Architect / Entrepreneur
2. AGENDA
• Memory Concepts
• Basic Principles
• Advantages and Disadvantages of using GC
• GC Algorithm
• Setting the context
• CLR implementation of GC
• Best practices related to memory management in .NET
3. MEMORY CONCEPTS
• Physical Memory: This is the physical RAM on the machine/device. This is mainly used by kernel
drivers.
• Virtual Memory: It’s the memory emulation Operating System does for user to store and access data.
Its not related to physical memory. Virtual Memory is used at system level.
• Address space: This is specific to the memory allocated/used by a process. Threads in a process use
address space for their stack, etc. Address space is used at process level.
• Private bytes: Private Bytes is the current size, in bytes, of memory that this process has allocated
that cannot be shared with other processes.
• Working Set: The Working Set is the set of memory pages touched recently by the threads in the
process.
4. ARE YOU GUYS AWAKE?
• Suppose user is running an application on a x64 bit machine.
• The machine has 4GB RAM installed.
• Task Manager shows 2GB of total RAM is used and rest is free
• After running application, it throws “Out Of Memory” (OOM) exception.
How?
5. OK, LET ME HELP YOU!
• Application always allocate memory in its “address space”. If the
allocated “address space” is exhausted, only then you’ll get OOM
exception even if Virtual Memory is available
6. BASIC PRINCIPLES!
• Find objects that cannot be accessed in the future
• Reclaim/Release the resources used by those objects
HISTORY
Garbage collection was invented by John McCarthy around 1959 to
solve problems in Lisp
7. ADVANTAGES AND DISADVANTAGES
+ Automatic memory reclaim of objects that are not required
+ Memory Leaks are taken care of (almost!)
+ Bugs like dangling pointers, double free, etc
+ Free developer from manually taking care of memory mgmt.
- Performance Impact; consume resources
- Pauses in application
- Non deterministic, not real time
9. GC ALGORITHM
• Reference Counting: Each object has a flag telling if it is referenced or not.
• Mark and Sweep: Two round collection cycle. First round it marks objects which are live
and in second round it clears the unused objects
• Copying Collectors: Create two heaps of same size. Use one for allocations; when
filled, pause the system and copy objects to other.
• Mark Compact Collector: Extended version of Mark and Sweep. In the second phase,
it compacts the heap to avoid fragmentation.
10. SETTING THE CONTEXT
• Reachability: GC depends on reachability heavily – to see if objects are reachable
or not.
• Roots: Starting point for GC to traverse the memory. Global & Static variables, etc
• Generations: Memory divided into generation. GC collects objects specific to
generation, avoiding full memory scan.
• Managed Heap: Process “continuous” address space which is governed by CLR/GC
• Determinism: GC are non-deterministic in the time of object finalization.
11. CLR IMPLEMENTATION OF GC
Object A
Object B
Object C
Managed Heap
NextObjPtr
Allocations
12. CLR IMPLEMENTATION OF GC
Image Source: MSDN
Allocated Objects in Heap
Managed Heap after Collection
Collection Algorithm
13. CLR IMPLEMENTATION OF GC
Image Source: MSDN
Finalization
Heap with multiple objects Managed Heap after Garbage Collection
14. CLR IMPLEMENTATION OF GC
Finalization Cont…
Managed Heap after Second Garbage Collection
# Objects with Finalize needs two GCs for cleanup.
Avoid having Finalize methods
# Finalize are different from destructor
# Any exception in Finalize should be handled and
return as if nothing happened. This helps other
Finalize method to execute
# Developers doesn’t control the execution (and the
order) of Finalize methods
# Finalizable objects should avoid referring to non-
Finalizable objects. Break the object into two types
and let the light weight have Finalize
15. BEST PRACTICES RELATED TO MEMORY
MANAGEMENT IN .NET
• Assigning NULL to an object when its not needed doesn’t collect the object instantly.
• While designing classes, if it has some unmanaged resources, do implement Dispose
Pattern and Finalize method
• In Dispose implementation, do call GC.SuppressFinalize method.
• If the class has Close/Dispose method, it’s the responsibility of the caller to call them.
Finalize methods are implemented here in case caller forgets to call them
• Avoid calling GC.Collect(..);
16. Q&A
Aim and shoot your questions!
Image source: http://egamer.co.za/2011/07/review-shadows-of-the-damned/