Java

Chapter 135: JVM Memory Management in Java — Heap, Stack, Metaspace, and Native Off-Heap

Map JVM memory regions: heap generations for objects, thread stacks and frames, metaspace for class metadata, direct buffers and native memory, and how flags like Xmx Xms relate to tuning.

Author: Sushil Kumar

Java JVM memory modelJava heap stack metaspaceJava Xmx Xms heap sizeJava native memory JVMJava JVM memory regions

Chapter 135: JVM Memory Management in Java

The JVM splits memory into regions with different lifetimes and collectors. Objects usually live in the heap; local variables and call frames live on per-thread stacks; class metadata moved from PermGen to Metaspace (mostly native) on modern JDKs; NIO direct buffers and JNI allocate off-heap memory still charged to your process.


1. Topic title

Memory management: know heap vs stack vs metadata vs native to read metrics and crashes


2. What it means

Heap — shared by all threads; young (Eden, Survivor) vs old (Tenured) conceptually describe object aging for generational collectors.

Stackprimitive and reference slots for active methods; depth limits prevent infinite recursion StackOverflowError.

Metaspaceclass metadata; -XX:MaxMetaspaceSize caps it—leaks from dynamic proxies still hurt.

Native memoryGC threads, JIT code cache, mapped jars—watch RSS vs heap only metrics.


3. Why it is used

Tuning Xmx/Xms balances tail latency vs cost on VMs.

Diagnosing OOME types—Java heap space, Metaspace, Direct buffer memorypoint to different fixes.


4. Mental sketch

Heap is the shared warehouse; stack is each worker’s clipboard; metaspace is the HR filing cabinet of who works here; native is the parking lot you still pay rent on even if warehouse looks empty.


5. Java code example

public class MemorySketch {
    private static final Object[] KEEP = new Object[1];
 
    public static void main(String[] args) {
        Runtime rt = Runtime.getRuntime();
        long heap = rt.maxMemory();
        System.out.println("max heap bytes ~ " + heap);
        KEEP[0] = new byte[1024 * 1024]; // 1 MiB object on heap
    }
}

Runtime.maxMemory reflects -Xmx ceilingactual usage oscillates with allocation rate and GC.


6. Explanation of code

totalMemory / freeMemory are coarseprefer MemoryMXBean or Micrometer jvm.memory.* for graphs.


7. Common mistakes

Setting Xmx == physical RAM`OS and native need headroomswap thrash follows.

Ignoring direct memory when tuning heap-onlyOutOfMemoryError: Direct buffer memory surprises Netty users.


8. Best practices

Capture -XX:+ExitOnOutOfMemoryError in containers so orchestrators restart cleanly.

Heap dumps (-XX:+HeapDumpOnOutOfMemoryError) on staging before first prod launch.


9. Small practice task

Run jcmd <pid> VM.native_memory summary on a local Spring Boot app and label top categories in your notes.


Beginner tip

top RES growing while heap flat often means metaspace, threads, or native allocatorsdo not blame GC first.