Fundamentals
-
Resources
-
Fundamentals of garbage collection - Memory allocation (opens in a new tab)
This is written for CLR but the concepts are applicable to JVM as well.
-
Algorithm
-
Generational hypothesis
-
It's faster to compact the memory for a portion of the managed heap than for the entire managed heap.
-
Newer objects have shorter lifetimes, and older objects have longer lifetimes.
-
Newer objects tend to be related to each other and accessed by the application around the same time.
-
A generational garbage collector logically separates the heap into 2 generations: a
young generation
and anold generation
. When an object is allocated, it is initially placed into theyoung generation
, which is frequently scanned. If an object survives long enough, it will be promoted to theold generation
.Generational garbage collectors perform this behavior to take advantage of the weak-generational hypothesis, which posits that most objects become unreachable shortly after they are created.
-
-
Minor GC
- occurs in the young generation.
- triggered when the young generation fills up.
- usually fast because it only collects the young generation.
-
Major GC
- occurs in the old generation.
- triggered when the old generation fills up.
- usually slow because it collects the old generation.
-
Stop the world event
- When a garbage collection stops all
application threads
.
- When a garbage collection stops all