Understanding Java Garbage Collection: Young Generation, Old Generation, and Permanent Generation
This article explains Java's garbage collection mechanisms, detailing the roles and algorithms of the young generation (copying algorithm), old generation (mark‑sweep and mark‑compact), and permanent generation, along with object promotion criteria, efficiency comparisons, and memory utilization considerations.
Young Generation Collection (Copying Algorithm)
In the heap, the young generation stores objects that are expected to be reclaimed quickly or are not particularly large (depending on the -XX:PretenureSizeThreshold setting). The copying algorithm divides the young generation into three regions: a large Eden space and two smaller Survivor spaces (default ratio Eden:Survivor = 8:1). Minor GC occurs in the young generation; during a Minor GC, surviving objects are copied into one Survivor space, and the Eden space together with the other Survivor space are cleared. Consequently, the usable young generation size equals the size of Eden plus one Survivor space.
Old Generation Collection (Mark‑Sweep / Mark‑Compact Algorithms)
The old generation holds objects that have survived multiple collections or are particularly large (again depending on -XX:PretenureSizeThreshold). It uses either the mark‑sweep or the mark‑compact algorithm, chosen by the JVM collector. Mark‑sweep can create significant memory fragmentation, whereas mark‑compact compacts live objects to eliminate gaps. Garbage collection in the old generation is called Major GC, and both Major GC and Full GC cause stop‑the‑world pauses.
Mark Phase : Traverse GC roots and mark all reachable objects.
Compact Phase : Move all live objects to be contiguous in memory, then reclaim the space after the last live object.
Promotion from Young to Old Generation
1. Allocation guarantee mechanism: during a Minor GC, if the surviving objects exceed the size of a Survivor space, the excess objects are promoted to the old generation.
2. If -XX:PretenureSizeThreshold is set (e.g., 3M), objects larger than this threshold are allocated directly in the old generation.
3. Each Minor GC increments the age of objects in the young generation; by default, objects that reach age 15 are promoted to the old generation, configurable via -XX:MaxTenuringThreshold.
Compared to the young generation, objects in the old generation are much harder to reclaim.
Permanent Generation Collection (Method Area)
The JVM's method area, also known as the permanent generation, stores class metadata, static variables, constants, and other class‑level data loaded by the virtual machine. Items in this region are even less likely to be reclaimed than those in the old or young generations.
Efficiency Comparison
Copying algorithm > Mark‑compact algorithm > Mark‑sweep algorithm (this reflects a simple time‑complexity comparison; real‑world performance may vary).
Memory Compaction
Copying algorithm = Mark‑compact algorithm > Mark‑sweep algorithm.
Memory Utilization
Mark‑compact algorithm = Mark‑sweep algorithm > Copying algorithm.
(java architecture journey) A platform dedicated to providing Java engineers with practical technical articles, guiding them from junior to senior levels on the path to becoming architects. More high‑quality Java articles are available via the QR code; feel free to share if you find this article helpful.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
