JVM Basics, Class Loading, Runtime Data Areas, Garbage Collection and Tuning Guide
This article provides a concise yet comprehensive overview of the Java Virtual Machine, covering its core components, how Java source files are compiled and executed, class‑loader mechanisms, runtime memory areas, garbage‑collection algorithms, and practical JVM tuning parameters for optimal performance.
The article introduces the Java Virtual Machine (JVM) as a specification‑based virtual computer that runs on top of an operating system, handling class loading, execution, and memory management.
1. Basic Introduction of JVM
JVM executes compiled .class bytecode files, interacting with the OS rather than hardware directly. The process from a HelloWorld.java source file to execution involves compilation, class loading, and method invocation.
1.1 How a Java File Is Run
A .java source file is compiled into a .class bytecode file, which the JVM can read. The class loader then loads the bytecode into the JVM.
① Class Loader
The class loader acts as a mover, transferring .class files into the JVM's runtime data area.
② Method Area
The method area stores class metadata, constants, static variables, and compiled code.
③ Heap
The heap holds objects and arrays; it is a thread‑shared region and is not thread‑safe.
④ Stack
Each thread has its own stack where method frames are executed.
⑤ Program Counter
The program counter is a thread‑local pointer to the next instruction to execute.
1.2 Simple Code Example
Example of a simple Student class and a main method that creates an instance and calls sayName(). The execution steps include loading the class, allocating heap memory, initializing the object, and invoking methods via the method area.
2. Class Loader Details
2.1 Class Loader Process
The lifecycle of a class consists of seven steps: loading, verification, preparation, resolution, initialization, usage, and unloading. Verification, preparation, and resolution together form the “linking” phase.
2.1.1 Loading
Load the .class file into memory.
Transform static data structures into runtime structures in the method area.
Create a java.lang.Class object representing the class.
2.1.2 Linking
Verification – ensure the class conforms to JVM specifications.
Preparation – allocate memory for static variables in the method area.
Resolution – replace symbolic references with direct references.
2.1.3 Initialization
Execute the class initializer <clinit>() to assign static values.
2.1.4 Unloading
Garbage collection removes classes that are no longer referenced.
2.2 Class Loader Loading Order
Bootstrap ClassLoader (loads rt.jar).
Extension ClassLoader (loads extension JARs).
Application ClassLoader (loads classpath JARs).
Custom ClassLoader (user‑defined).
2.3 Parent‑Delegation Model
When a class loading request arrives, the request is delegated to the parent loader first; only if the parent cannot find the class does the child attempt to load it, ensuring a single definition for core classes.
3. Runtime Data Areas
3.1 Native Method Stack and Program Counter
Native methods (marked native) run in the native method stack, while the program counter points to the next bytecode instruction and never triggers OutOfMemoryError.
3.2 Method Area
Stores class metadata, constants, and static variables; overflow leads to errors.
3.3 Java Stack and Heap
The stack executes code, the heap stores objects. The stack holds local variables, operand stacks, and frames; the heap is shared among threads.
3.3.1 Java Stack Concept
Each thread has its own stack storing local variables, dynamic links, and return addresses.
3.3.2 Stack Exceptions
Exceeding stack depth causes StackOverflowError; insufficient native memory causes OutOfMemoryError.
3.3.3 Heap Structure
The heap is divided into Young Generation (Eden + Survivor spaces) and Old Generation; after Java 8 the Permanent Generation was replaced by Metaspace.
3.3.4 Garbage Collection Algorithms
Common algorithms include Mark‑Sweep, Copying, Mark‑Compact, and Generational collection, each with trade‑offs regarding fragmentation and throughput.
4. JVM Tuning Guidelines
4.1 Adjust Max and Min Heap Size
Use -Xmx and -Xms to set maximum and initial heap sizes; keeping them equal avoids repeated resizing.
4.2 Adjust Young/Old Generation Ratio
Use -XX:NewRatio to control the proportion of young to old generation (e.g., -XX:NewRatio=4 makes young generation 1/5 of the heap).
4.3 Adjust Survivor/Eden Ratio
Use -XX:SurvivorRatio (e.g., 8) to set Eden:Survivor = 8:1.
4.4 Set Young Generation Size Directly
Parameters -XX:NewSize and -XX:MaxNewSize control the absolute size of the young generation.
4.5 Example JVM Options
-Xmx20m -Xms5m -XX:+PrintGCDetailsRunning a program with these options shows allocation failures, GC logs, and memory usage changes.
4.6 Permanent Generation / Metaspace Settings
Use -XX:PermSize and -XX:MaxPermSize (pre‑Java 8) or Metaspace parameters ( -XX:MetaspaceSize, -XX:MaxMetaspaceSize) to control class metadata memory.
4.7 Thread Stack Tuning
Adjust per‑thread stack size with -Xss or -XX:ThreadStackSize to balance thread count and stack overflow risk.
4.8 Miscellaneous Tuning Flags
Examples include -XX:+UseBiasedLocking (lock optimization), -XX:+AggressiveOpts (enable experimental optimizations), and -XX:+DisableExplicitGC (ignore System.gc() calls).
Finally
The article aggregates knowledge from various sources, including books and online courses, to give readers a practical understanding of JVM internals and tuning techniques.
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.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
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.
