Fundamentals 16 min read

Mastering JVM Memory: Deep Dive into Models, GC Algorithms, and Tuning

This comprehensive guide explains the JVM memory model, details each memory region, compares major garbage‑collection algorithms, and lists essential JVM tuning parameters for optimal Java performance.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mastering JVM Memory: Deep Dive into Models, GC Algorithms, and Tuning
Regardless of BAT interviews or JVM tuning at work, understanding the JVM memory model, allocation, and garbage‑collection algorithms is essential.

JVM Memory Model

The JVM memory model consists of two parts: the shared heap and method area, and the thread‑private virtual machine stack, native method stack, and program counter.

1. Heap

The heap is shared by all threads and is divided into the young generation and old generation. The former includes Eden, Survivor0, and Survivor1 (ratio 8:1:1). Heap size is controlled by -Xms (initial) and -Xmx (maximum). The JVM automatically expands or shrinks the heap based on usage thresholds.

2. Method Area (Method Area)

The method area, also called the “permanent generation,” stores class metadata, constants, and static variables. Its size can be limited with -XX:PermSize and -XX:MaxPermSize. When the area exceeds its limit, an OOM error occurs.

3. JVM Stack

Each Java method creates a stack frame that holds a local variable array, an operand stack, and a frame data area. The stack is thread‑private and follows a last‑in‑first‑out discipline.

4. Native Method Stack

Similar to the JVM stack but dedicated to native (C/C++) method calls.

5. Program Counter (PC Register)

The smallest memory region, indicating the current bytecode line for each thread.

6. Direct Memory

Direct memory is allocated outside the JVM heap via NIO and is not part of the JVM‑defined memory areas.

JVM Garbage‑Collection Algorithms

1. Mark‑Sweep

Scans reachable objects from root set, marks them, then sweeps away unmarked objects. Efficient when many objects are dead, but creates fragmentation.

2. Copying Algorithm

Copies live objects to a new space and discards the old one. Best for young generation where most objects die quickly; requires free space equal to the live set.

3. Mark‑Compact

Marks live objects, then compacts them to eliminate fragmentation. Suitable for old generation but incurs extra copying overhead.

4. Generational Collection

Divides the heap into young and old generations, applying the most appropriate algorithm to each based on object lifespan.

Garbage Collectors

1. Serial Collector

Single‑threaded, stops all application threads during collection; optimal for single‑CPU environments.

2. ParNew Collector

Multithreaded version of Serial for the young generation; still stops the world during collection.

3. Parallel Scavenge Collector

Uses the copying algorithm with multiple threads, aiming for high throughput.

4. CMS Collector

Concurrent Mark‑Sweep focuses on minimizing pause times; performs initial and final marks with stop‑the‑world, while concurrent marking and sweeping run alongside application threads. May cause fragmentation.

5. G1 Collector

Region‑based, parallel and concurrent collector designed for large heaps; provides predictable pause times and reduces fragmentation.

Common GC‑Related JVM Parameters

-Xmx

: maximum heap size -Xms: initial heap size -Xmn: young generation size -Xss: thread stack size -XX:PretenureSizeThreshold: size threshold for direct promotion to old generation -XX:MaxTenuringThreshold: object age threshold for promotion -XX:+UseAdaptiveSizePolicy: enables automatic tuning of generation sizes and other parameters -XX:SurvivorRatio: ratio of Eden to Survivor spaces (default 8:1) -XX:ParallelGCThreads: number of GC threads -XX:MaxGCPauseMillis: target maximum GC pause time -XX:GCTimeRatio: target GC overhead as a percentage of total time

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMMemory ManagementGarbage Collectionperformance tuning
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.