2. In computer science, garbage collection (GC) is a
form of Automatic Memory Management.
The garbage collector attempts to reclaim the
memory occupied by objects that are no longer in
use by the program.
Garbage collection was invented by John McCarthy
around 1959 to abstract away manual memory
management in Lisp.
3. Static vs Dynamic memory allocation
Creating an array of ten integers in C (static)
int array[10];
Creating an array of integers in C (dynamic)
int * array = malloc(N * sizeof(int));
free(array);
In C++
Foo* fooPtr = new Foo()
delete fooPtr
4. Languages without GC
C, C++*, D*, Objective-C*,
Rust*
Languages
Languages wit GC
Java, Go, PHP, Python, Scala*,
Haskell, ….
6. Reference Counting
Keep an extra integer (“reference count”) to every
heap-allocated data structure.
● With a new reference ++refCount.
● When a reference disappears --refCount.
● If refCount == 0, then reclaim the storage
+ Easy to implement
+ Real Time cleanup
- Additional storage
- Speed: incr, decr
- Cycles not cleaned
C++, Objective-C, Rust,
PHP, Python….
8. Mark and Sweep
Mark Phase: traverses all objects,
starting with roots, and marks every
object found as alive.
Sweep Phase: traverses all
objects, reclaim the storage of
unmarked objects.
+ Handles cycles
+ Easy to implement
- Stops the world**
- Scans the entire heap
10. Generational Collection
● Most objects die young
● Newer objects usually point to older objects
+ Same as Copy (unidirectional)
+ Less Objects to Copy
+ Frequency of collection
+ Faster
- Stops the world**
- Complex to implement
13. Java Garbage Collectors
● Serial Collector - -XX:+UseSerialGC
● Parallel Collector - -XX:+UseParallelGC
● Concurrent Mark & Sweep Collector - -XX:+UseConcMarkSweepGC
● Garbage First (G1) Collector - -XX:+UseG1GC
14. Serial GC
● Stops the world
● Uses single thread
● Designed for single CPU small Heap apps
● Do not use it!
● Young Gen - Copy Collector
● Old Gen - Mark and Sweep
15. Parallel GC (The Default Collector)
● Stops the world
● Designed to work with multiple CPUs
● Uses multiple threads
● Expect high latencies when GC runs (-XX:MaxGCPauseMillis=<N>)
● Young Gen - Copy Collector
● Old Gen - Mark and Sweep
16. Concurrent Mark and Sweep (CMS) GC
● Stops the world (relatively short pauses)
● Designed to work with multiple CPUs
● Uses multiple threads
● Good for low latency apps
● Young Gen - Copy Collector
● Old Gen - Concurrent Mark-Sweep
17. Garbage First (G1) Collector
● Heap is split into (typically 2048) smaller regions
● Avoids collecting the entire heap at once, instead collects incrementally
○ The regions that contain the most garbage are collected first
● Soft real-time garbage collector (Predictable | Configurable STW)
● Compaction is relatively easy
● Uses multiple threads and is good for > 6G heap sizes. Java 9 Default GC.
● Young Gen - Copy Collector
● Old Gen - Concurrent Mark-Sweep
18. Minor GC vs Major GC vs Full GC
Minor GC cleans the Young Generation
Major GC cleans the Old Generation
Full GC cleans the Young and Old Generation