Comprehensive Guide to Java JVM: Architecture, Class Loading, Garbage Collection, and Performance Tuning

This article provides an in‑depth overview of the Java Virtual Machine, covering JDK/JRE/JVM relationships, compilation process, class loading mechanisms, the parent‑delegation model, runtime memory areas, garbage collection algorithms and collectors, and practical JVM performance tuning tips for backend developers.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Comprehensive Guide to Java JVM: Architecture, Class Loading, Garbage Collection, and Performance Tuning

Introduction

As a Java developer, understanding the JVM is essential. This article summarizes core JVM concepts, presents a mind map, and offers 18 key questions to test your knowledge.

JDK, JRE, JVM Relationship

JVM (Java Virtual Machine) executes .class files. JDK (Java Development Kit) provides development tools, while JRE (Java Runtime Environment) supplies the runtime needed to run Java programs. The hierarchy is JDK > JRE > JVM.

Compilation Process

The source code is compiled into .class files through four steps:

Lexical analysis – tokenizing the source code.

Syntax analysis – building an abstract syntax tree.

Semantic analysis – simplifying and annotating the AST.

Bytecode generation – producing the .class file.

Class Loading

Class loading involves locating the binary byte stream of a class, converting it into a runtime data structure, and creating a java.lang.Class object in the heap.

Ways to Load .class Files

From the local file system.

From JAR/WAR packages.

From network or database.

Dynamic compilation of source files.

Class Loading Mechanism

Three phases: [Loading] – import the class file; [Linking] – verification, preparation, and resolution; [Initializing] – create class instances (e.g., new User()).

Verification

Ensures class file correctness (format, metadata, bytecode, symbolic references).

Preparation

Allocates memory for static variables and initializes them to default values.

Resolution

Transforms symbolic references into direct references.

Class Loaders

Four main loaders:

Bootstrap ClassLoader – loads core JDK classes from $JAVA_HOME/jre/lib/rt.jar.

Extension ClassLoader – loads extension JARs.

App ClassLoader – loads classes from the application classpath.

Custom ClassLoader – user‑defined loaders (e.g., Tomcat’s WebAppClassLoader).

Key methods in java.lang.ClassLoader:

public Class<?> loadClass(String name) throws ClassNotFoundException { return loadClass(name, false); }
protected Class<?> findClass(String name) throws ClassNotFoundException { throw new ClassNotFoundException(name); }
protected final Class<?> defineClass(String name, byte[] b, int off, int len, ProtectionDomain pd) throws ClassFormatError { /* ... */ }

Parent Delegation Model

When a class loader receives a load request, it first delegates to its parent. Only if the parent cannot load the class does the loader attempt to load it itself. This model ensures security and prevents duplicate class definitions (e.g., multiple java.lang.Object copies).

Breaking the Model

Tomcat’s WebAppClassLoader loads web‑app classes before delegating, allowing different applications to use different library versions. OSGi also uses custom loaders to enforce module boundaries.

Runtime Data Areas

Program Counter (PC) Register
Java Virtual Machine Stack
Heap
Method Area
Native Method Stack

Method Area

Stores class metadata, static variables, and compiled code. Implemented as Permanent Generation (pre‑JDK8) or Metaspace (JDK8+).

Heap

Divided into Young Generation (Eden, From Survivor, To Survivor) and Old Generation. Objects are allocated in Eden; surviving objects move to Survivor spaces and eventually to Old Generation.

Garbage Collection Algorithms

Common algorithms:

Mark‑Sweep – marks live objects then sweeps dead ones (produces fragmentation).

Copying – copies live objects to another space (used in Young Generation).

Mark‑Compact – moves live objects to one end, eliminating fragmentation.

Garbage Collectors

Collectors implement the algorithms for specific generations:

Serial – single‑threaded, uses copying for Young Gen.

ParNew – multithreaded version of Serial.

Parallel Scavenge – focuses on throughput, multithreaded copying.

CMS (Concurrent Mark‑Sweep) – low‑pause collector for Old Gen.

G1 – region‑based, aims for predictable pause times.

ZGC – ultra‑low pause (<10 ms), region‑based, uses colored pointers and read barriers.

JVM Performance Tuning

Key tuning areas include heap size ( -Xms, -Xmx), Young/Old generation ratios ( -XX:NewRatio, -XX:SurvivorRatio), GC logging ( -XX:+PrintGCDetails), and selecting appropriate collectors ( -XX:+UseG1GC, -XX:+UseConcMarkSweepGC).

Common Parameters

-Xms

and -Xmx – set initial and maximum heap size (often set equal). -XX:NewSize / -XX:NewRatio – configure Young Generation size. -XX:MaxMetaspaceSize – limit Metaspace. -XX:ParallelGCThreads – number of GC threads. -XX:MaxGCPauseMillis – target maximum pause time.

Diagnostic Tools

jps

– list JVM processes. jstat – monitor GC and memory statistics. jmap – dump heap or view histogram. jconsole / jvisualvm – visual monitoring.

Third‑party: MAT, GCViewer, JProfiler, Arthas.

Best Practices

Avoid large short‑lived objects; they may cause premature promotion to Old Gen.

Use soft/weak references for caches to allow GC reclamation.

Clear collections promptly to prevent memory leaks.

Prefer asynchronous processing for long‑running I/O.

Conclusion

The article walks through the entire JVM lifecycle—from JDK/JRE/JVM basics, compilation, class loading, memory layout, garbage collection algorithms and collectors, to practical tuning and tooling—providing a solid foundation for backend developers to understand and optimize Java applications.

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.

JavaJVMGarbage Collectionperformance tuningclass loading
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

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.