Backend Development 33 min read

Understanding JVM Memory, GC, and Performance Tuning

The article explains JVM runtime memory regions, heap allocation and generational layout, object reference types, major garbage‑collection algorithms and collectors, class‑loading phases, and provides practical tuning commands and case studies for optimizing GC pauses, preventing memory leaks, and diagnosing performance bottlenecks.

DaTaobao Tech
DaTaobao Tech
DaTaobao Tech
Understanding JVM Memory, GC, and Performance Tuning

This article provides a comprehensive overview of the Java Virtual Machine (JVM) runtime data areas, including the heap, method (or metaspace) area, virtual machine stack, native method stack, and program counter.

It explains how objects are allocated in the heap, the division of the heap into young and old generations, and the role of Eden and Survivor spaces. It also describes object allocation strategies, object header layout, and the steps performed when creating a new object (class loading check, memory allocation, zero‑initialization, setting the object header, and invoking the init method).

The article details the different types of object references (strong, soft, weak, phantom) and how they affect garbage collection.

Garbage collection algorithms are covered, including copying (young generation), mark‑sweep, mark‑compact, and generational collection. It distinguishes MinorGC, MajorGC, and FullGC, and explains the conditions that trigger each.

Various garbage collectors across JDK versions are compared (Serial, ParNew, Parallel Scavenge, CMS, G1, ZGC, Shenandoah) with their goals (throughput, low pause time, large heap support) and typical use‑cases.

JVM class loading phases—loading, verification, preparation, resolution, and initialization—are described, along with the double‑parent delegation model and how Tomcat’s WebAppClassLoader intentionally breaks this model.

Performance‑tuning commands are listed, with examples wrapped in code tags: jps -l , jps -m , jps -v , jinfo 11666 , jinfo -flags 11666 , jstat -gc 12345 1000 10 , jstack -l 12345 , jmap -heap 12345 , jmap -dump:format=b,file=heap.bin 12345 .

The article also discusses practical JVM tuning cases: adjusting MaxTenuringThreshold , enabling CMSScavengeBeforeRemark , using ParallelRefProcEnabled for weak‑reference processing, and switching to G1 with a pause‑time target of 200 ms.

Memory‑leak diagnostics using MAT, analysis of large HashMap caches without eviction policies, and proper use of Guava caches with weak keys are presented.

Additional sections cover troubleshooting steps for CPU spikes, high GC pause, swap usage, and I/O bottlenecks, emphasizing the importance of disabling swap on high‑performance Java services.

JavaJVMGarbageCollectionMemoryManagementPerformanceTuning
DaTaobao Tech
Written by

DaTaobao Tech

Official account of DaTaobao Technology

0 followers
Reader feedback

How this landed with the community

login 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.