Understanding JVM Memory Areas and Garbage Collection Mechanisms
This article explains the JVM's memory layout, including method area, heap, stack, PC register, and native method stack, and details garbage collection concepts such as marking, deletion, compaction, generational GC, and the characteristics of various collectors like Serial, Parallel, CMS, and G1.
Using Java for nearly a year, the author reflects on the language's conveniences and stresses the importance of mastering the JVM's garbage collection (GC) mechanisms.
JVM Runtime Data Areas
The JVM memory is divided into five main regions:
1. Method Area
Stores class metadata such as class names, types, fields, and methods.
2. Heap
All objects created with new, this, and arrays reside here; it is managed by the GC.
3. Stack
Each thread has its own stack storing method call frames, local variables, and temporary data.
4. PC Register
Holds the address of the current executing instruction for each thread.
5. Native Method Stack
Executes native code (e.g., C/C++) separate from the Java stack.
Introduction to GC
GC automatically finds and reclaims unused objects in the heap. The process includes:
Marking
All reachable objects are identified and marked.
Normal Deletion
Marked objects are removed from memory.
Deletion with Compacting
After removal, remaining live objects are compacted to eliminate fragmentation.
GC roots (objects directly reachable from thread stacks, static fields, constants, and native stacks) define the starting points for reachability analysis. Java defines four reference types: strong, soft, weak, and phantom.
Generational GC and Mechanisms
The heap is split into Young Generation (Eden + two Survivor spaces) and Old Generation (Tenured). Minor GC cleans the Young Generation, while Major (Full) GC cleans the Old Generation.
Young Generation
New objects are allocated in Eden. When Eden fills, a Minor GC moves surviving objects to a Survivor space and clears the rest.
Repeated Minor GCs eventually promote objects that survive a configurable number of cycles (default 15) to the Old Generation.
The Young Generation uses a Copying GC algorithm (From/To spaces) to achieve high throughput.
void copying(){
$free = $to_start // $free indicates the offset in To space
for(r : $roots)
*r = copy(*r) // copy and return new reference
swap($from_start, $to_start) // exchange From and To after GC
}Old Generation
Stores long‑living objects; Major GC (Full GC) reclaims space here. Initially a Mark‑Sweep algorithm was used, but modern JVMs employ Mark‑Compact to reduce fragmentation.
Permanent Generation
Part of the Method Area that holds class metadata; it has little impact on GC of regular objects.
GC Collectors and Optimization
When evaluating GC collectors, consider throughput, overhead, pause time, frequency, heap size, and object lifetimes.
Serial – single‑threaded, uses Mark‑Copy; long pauses.
Parallel – multi‑threaded, focuses on throughput; suitable for CPU‑bound workloads.
CMS – Concurrent Mark‑Sweep; minimizes pause times for latency‑sensitive services.
G1 – Garbage‑First; divides heap into regions, uses Mark‑Compact, reduces Full GC frequency for large heaps.
Common JVM flags:
-XX:+UseSerialGC
-XX:+UseParNewGC
-XX:+UseParallelGC
-XX:+UseParallelOldGC
-XX:ParallelGCThreads=n
-XX:+UseConcMarkSweepGC
-XX:ParallelCMSThreads=n
-XX:+UseG1GCOverall, selecting and tuning the appropriate GC algorithm depends on the application’s performance goals and workload characteristics.
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.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.
