JVM GC
Algorithm
Mark-Sweep
First Mark all cleanable memory, then Clean at one time
- Efficiency problem
- Memory fragmentation
Copying
Use when surviving rate is low(Usually it is the case)
- Divide the entire memory into 2 sections
- Use only one of the section as memory allocation section.
- Once that allocation section is exhausted, move the part that are still being used onto the top of the other memory section
- Cleaning the allocation section.
In this way, there is no memory fragmentation, and cleaning is quite efficient.
Major commercial JVM use this strategy
- Memory usability is 50% only
Mark-Compact
Use when surviving rate is high.
- Just like
Mark-Sweep
- But
compact
the memory. Move the memory into one direct so that there is no memory fragment
Generational
Major commercial JVM use this strategy.
Must use along with other strategy.
Collector
Serial
Must stop all the other works when collecting, which is so bad.
ParNew
Multi-thread version of Serial
collector, nothing new.
Parallel Scavenger
Similar to ParNew but this collector focuses on reaching a controllable throughout.
Sometimes also called Throughout first collector
$$Throughout = \frac{User\ run\ time}{User\ run\ time + GC\ time} $$
Concurrent Mark Sweep
To collect as fast as possible.
- Init mark(Stop the world)(Only mark objects that are directly GC related)
- Concurrent mark(GC Roots tracing)
- Remark(Stop the world)(Check objects that changed during concurrent mark)
- Concurrent sweep
Great collector.
Sensitive to CPU
G1
Art of the state collector.
Based on Mark-Compact
Maintain a priority queue of garbage, collect the garbage first to ensure good throughout.