Comprehensive Overview of JVM Memory Model, Garbage Collection Algorithms, and Tuning Parameters
This article provides an in‑depth explanation of the JVM memory model, detailing heap, method area, stacks, direct memory, various garbage‑collection algorithms, collector types, and common JVM tuning parameters for performance optimization.
JVM Memory Model
JVM memory is divided into two parts: shared areas (heap and method area) used by all threads, and thread‑private areas (virtual machine stack, native method stack, and program counter).
1. Heap
The heap is a shared memory region divided into young and old generations; the former contains Eden and two Survivor spaces (ratio 8:1:1). Heap size is controlled by -Xms (initial) and -Xmx (maximum) parameters, typically set to a fraction of physical memory.
Heap objects are allocated in the young generation; after surviving a configurable number of Minor GCs they are promoted to the old generation. The old generation typically uses a mark‑compact algorithm.
2. Method Area
The method area (formerly the permanent generation) stores class metadata, constants, and static variables. Its size can be limited with -XX:PermSize and -XX:MaxPermSize, but it was removed in JDK 8 in favor of Metaspace.
3. Virtual Machine Stack
Each Java method invocation creates a stack frame containing a local variable table, an operand stack, and frame data. The stack is thread‑private and its size is set by -Xss.
4. Native Method Stack
Similar to the VM stack but used for native (JNI) method execution; its size is generally much smaller than the heap.
5. Program Counter (PC) Register
The PC register holds the address of the next bytecode instruction to execute for each thread.
6. Direct Memory
Direct memory is off‑heap memory allocated via NIO; it is not part of the JVM heap and does not affect -Xmx limits.
Garbage Collection Algorithms
1. Mark‑Sweep
Scans from root objects, marks live objects, then sweeps away unmarked ones. Efficient when many objects survive, but can cause fragmentation.
2. Copying (Copy‑On‑Write)
Copies live objects from one region to another, then discards the original region. Works well for the young generation where most objects die quickly.
3. Mark‑Compact
Marks live objects, then compacts them to eliminate fragmentation. Suitable for the old generation but requires two full scans.
4. Generational Collection
Divides the heap into generations (young and old) and applies different algorithms to each, exploiting the fact that most objects die young.
Garbage Collectors
1. Serial Collector
Single‑threaded collector, default for client‑mode JVMs; pauses all application threads during collection.
2. ParNew Collector
Multithreaded version of Serial for the young generation; still stops the world during collection.
3. Parallel Scavenge Collector
Multithreaded copying collector focused on maximizing throughput.
4. CMS Collector
Concurrent Mark‑Sweep aims for low pause times; uses mark‑sweep for the old generation and can suffer from fragmentation.
5. G1 Collector
Region‑based collector designed for large heaps; provides predictable pause times, parallelism, and concurrent phases.
Common JVM Tuning Parameters
-Xmx: maximum heap size.
-Xms: initial heap size.
-Xmn: size of the young generation.
-Xss: thread stack size.
-XX:ParallelGCThreads: number of GC threads.
-XX:MaxGCPauseMillis: target maximum GC pause time.
-XX:GCTimeRatio: desired ratio of GC time to application time.
-XX:SurvivorRatio: ratio of Eden to Survivor space sizes.
-XX:+UseAdaptiveSizePolicy: enables automatic adjustment of generation sizes and other parameters.
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.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
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.
