JVM Garbage Collection (GC)

Java Heap Memory



The Young Generation is where all new objects are allocated and aged.
Tenure Generation stores old objects, which initially in young generation space, aged and moved to.
Permanent Generation Space stores metadata required by JVM to describe classes and methods used in the application.

JVM Garbage Collection 

Minor garbage collection (quick, stop-the-world)

All objects started at young generation space, Eden space. When Eden space is full, GC runs in Eden Space, selecting all survivor objects and move them to Survivor Space 0. When Eden space full again, GC runs in Eden Space and Survivor Space 0, selecting survivor and move them to Survivor Space 1. When Eden space full again, GC runs in Eden Space and Survivor Space 1, selecting survivor and move them to Survivor Space 0.So and so.
Every time an object moved from one survivor space to another survivor space, they age. When their age reach 8 (JVM 8), they are moved to Tenure Generation Space.
GC strategy for minor garbage collection is Copying Collector.

Major garbage collection (conccurently)

When Tenured Generation filled up and to be cleaned up, major GC happens.
Major GC includes GC at Young Generation, following by GC at Tenured Generation.
GC strategy for Tenured Generation is Mark and Swipe, or Mark and Compact.

Full garbage collection (long, stop-the-world)

When Permanent Generation filled up and to be cleaned up, full GC happens.
Permanent GC includes GC at Permanent Generation, as well as GC at Young Generation and GC at Tenured Generation.
GC strategy for Tenured Generation is Mark and Swipe, or Mark and Compact.

Extra Questions and Answers

Q: After compaction at Copying Collection, the whole survival objects collection size are bigger than Survivor 0, what will happen?
A: The current live objects which are not fit into Survivor 0/ Survivor 1, firstly they are moved to tenured section, which is dangerous because they didn't got moved because of age. Then the size of Survivor 0/ Survivor 1 adjusted using a ratio parameter.

Good Reading Material

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html 

Comments