Understanding JVM Heap Memory: Structure, Configuration, and Garbage Collection
This article explains the JVM heap memory area, its default and configurable sizes, the generational layout (Eden, Survivor, Old), how objects are allocated and promoted, and the various garbage‑collection mechanisms and JVM parameters that affect performance and memory usage.
Heap Overview
The JVM heap is the core memory region of the runtime data area, shared by all threads. A Java program runs in a single process, which hosts one JVM instance; each JVM instance has one runtime data area and multiple threads.
Heap Size Settings
When the heap is created, its initial size defaults to 1/64 of physical memory and the maximum size to 1/4 of physical memory, but both can be adjusted using JVM options.
Tools
Use the JDK‑bundled jvisualvm tool with the Visual GC plugin to monitor heap usage.
Code Sample
public class HeapDemo {
public static void main(String[] args) {
System.out.println("start...");
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end...");
}
}JVM Parameters
-Xms10msets the initial heap size (equivalent to -XX:InitialHeapSize). -Xmx10m sets the maximum heap size (equivalent to -XX:MaxHeapSize).
The -X prefix denotes JVM runtime options; ms means memory start.
Setting -Xms and -Xmx to the same value prevents the JVM from resizing the heap during garbage collection, improving performance.
Heap Generations
Objects are divided into short‑lived (young) and long‑lived (old) categories. The young generation consists of Eden, Survivor0 (S0) and Survivor1 (S1) spaces; the old generation holds objects that survive multiple collections.
Generation Ratios
Default -XX:NewRatio=2 gives the young generation 1/3 of the heap.
Changing to -XX:NewRatio=4 makes the young generation 1/5 of the heap.
Eden and Survivor Ratios
In HotSpot, Eden, Survivor0, and Survivor1 typically have an 8:1:1 ratio (can be tuned with -XX:SurvivorRatio).
Object Allocation Process
New objects are allocated in Eden; if Eden is full, a Minor GC is triggered.
If the object still does not fit, it is allocated directly in the old generation; if that fails, a Full GC occurs.
Objects that survive enough Minor GCs are promoted to the old generation (promotion).
Garbage Collection Types
Partial GC
Collects only part of the heap:
Young generation (Minor/Young GC)
Old generation (Major GC)
Mixed GC (e.g., G1 GC collects young and some old regions)
Full GC
Collects the entire Java heap and the method area (perm‑gen or metaspace).
GC Trigger Mechanisms
Young Generation (Minor GC)
Triggered when Eden is full; Survivor spaces do not directly cause a GC.
Frequent due to the short lifespan of most Java objects.
Causes a Stop‑The‑World pause while the GC runs.
Old Generation (Major/Full GC)
Occurs when the old generation runs out of space, often after a Minor GC.
Major GC is slower than Minor GC, with longer STW pauses.
If space is still insufficient after Major GC, an OutOfMemoryError (OOM) is thrown.
Full GC Conditions
Explicit call to System.gc() (suggested, not guaranteed).
Old generation or metaspace exhaustion.
Insufficient space after promoting objects from Young to Old.
Large objects cannot fit into Survivor spaces and must be moved to Old.
Full GC should be avoided during development and tuning because its pause time is much longer.
Additional Resources
Recommended reading links are provided for further study on database isolation levels, MySQL architecture evolution, and MySQL tuning.
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.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.
