JVM Overview: Components, Runtime Data Areas, Class Loading, Garbage Collection, and Tuning
This article provides a comprehensive overview of the Java Virtual Machine, detailing its main components, runtime data areas, class loading mechanism, garbage collection algorithms and collectors, heap vs stack differences, object reclamation criteria, and common JVM tuning tools and parameters.
JVM Main Components
ClassLoader – loads Java classes and converts them to bytecode.
Runtime Data Area – memory structures where bytecode is executed.
Execution Engine – interprets or JIT‑compiles bytecode into native instructions.
Native Interface – allows the JVM to call native libraries.
Runtime Data Area Sub‑areas
Program Counter (PC) register
Java Virtual Machine Stack
Native Method Stack
Heap
Method Area (Metaspace)
Some areas exist for the lifetime of the JVM process, while others are created and destroyed with each user thread.
Heap vs. Stack
Stack stores local variables; heap stores objects.
Stack allocation is faster and its lifetime is short; heap objects are reclaimed by the garbage collector.
When a stack frame ends, its variables are released automatically; heap objects are reclaimed when they become unreachable.
Queue and Stack
Both are data‑storage structures.
Queue follows FIFO (first‑in‑first‑out) semantics, with Deque allowing double‑ended access.
Stack follows LIFO (last‑in‑first‑out) semantics.
Parent‑Delegation Model
Class loading follows a hierarchy: a child class loader delegates the loading request to its parent before attempting to load the class itself. The bootstrap class loader loads core Java libraries, followed by extension and application class loaders.
Class Loading Process
Loading – locate and read the .class file.
Verification – ensure the class file is structurally correct.
Preparation – allocate memory for static fields.
Resolution – replace symbolic references with direct references.
Initialization – execute static initializers and <clinit> blocks.
Object Reclamation
Reference counting – objects with a zero count are eligible for collection (cannot handle cycles).
Reachability analysis – objects not reachable from GC roots are considered garbage.
Java Reference Types
Strong
Soft
Weak
Phantom (ghost) references
Garbage‑Collection Algorithms
Mark‑Sweep
Mark‑Compact
Copying
Generational
Garbage‑Collection Implementations
Serial and Serial Old (single‑threaded)
ParNew (multithreaded version of Serial)
Parallel and Parallel Old (throughput‑oriented)
CMS – Concurrent Mark‑Sweep, low‑pause collector
G1 – balanced throughput and pause time, default since JDK 9
CMS Details
CMS aims for minimal pause time at the cost of throughput; it uses the mark‑sweep algorithm, can cause fragmentation, and may fall back to Serial Old when memory is insufficient.
Young vs. Old Generation Collectors
Young: Serial, ParNew, Parallel Scavenge (usually copying algorithm)
Old: Serial Old, Parallel Old, CMS (typically mark‑compact)
Whole‑heap: G1
Young generation uses a copying algorithm with Eden, From‑Survivor, and To‑Survivor spaces (default ratio 8:1:1). Objects surviving a configurable number of collections (default 15) are promoted to the old generation.
JVM Tuning Tools
jconsole – monitors memory, threads, and class loading.
jvisualvm – provides memory/CPU snapshots, thread analysis, deadlock detection, and GC monitoring.
Common JVM Tuning Parameters
-Xms2g – initial heap size.
-Xmx2g – maximum heap size.
-XX:NewRatio=4 – ratio of old to young generation.
-XX:SurvivorRatio=8 – Eden to Survivor space ratio.
-XX:+UseParNewGC – enable ParNew + Serial Old collectors.
-XX:+UseParallelOldGC – enable Parallel Old collector.
-XX:+UseConcMarkSweepGC – enable CMS + Serial Old.
-XX:+PrintGC / -XX:+PrintGCDetails – print GC logs.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.