Mastering JVM Class Loading: Key Concepts, Models, and Practical Tips
This article explains the JVM class loading mechanism, covering the loading and unloading process, lifecycle stages, the parent‑delegation model, timing of loading and unloading, practical debugging with -verbose:class, and the impact of custom class loaders on application behavior.
In Java interview scenarios, after discussing project experience and basic technologies, interviewers often probe deeper into JVM knowledge, especially the class loading mechanism, which is a classic topic.
Briefly describe the JVM class loading process.
When are classes loaded and unloaded?
How to understand different class loader concepts and their roles?
Explain the JVM parent‑delegation model.
What situations break the parent‑delegation model and why? Provide an example.
Do you know Tomcat's class loading mechanism and its design rationale?
What class‑loader‑related issues have you encountered in real development and how did you solve them?
How do weakly‑typed languages like Groovy implement dynamic class loading?
In the following series, the author will revisit class‑loader knowledge to answer the above questions.
Basic Concepts
Class Loading and Unloading
The JVM executes bytecode contained in .class files. Java source code is compiled into bytecode, which the JVM loads into memory and runs; this is called class loading . Conversely, removing a class's runtime data from the JVM is called class unloading .
Runtime data for class files are stored in the permanent generation (PermGen) before JDK 7 and in Metaspace after JDK 8.
Class Lifecycle
A Java class goes through seven phases from loading to unloading: Loading, Verification, Preparation, Resolution, Initialization, Using, and Unloading. Verification, Preparation, and Resolution together form the Linking phase.
Timing of Class Loading
The JVM specification does not strictly define when a class is loaded; it depends on the specific JVM implementation. Loading typically occurs when the constant pool is resolved, or when methods like Class.forName(), ClassLoader.loadClass(), reflection APIs, or JNI are invoked. The JVM also loads classes during its own startup.
Using the JVM option -verbose:class prints the class‑loading process at application start:
-verbose:classClass Initialization Conditions
The JVM initializes a class in five situations:
When an object is instantiated with new, or when a static field is read/written, or when a static method is invoked.
When reflection (via java.lang.reflect) accesses the class and it has not been initialized yet.
When a class is being initialized and its superclass has not yet been initialized.
When the JVM starts and the user‑specified main class (containing main) is loaded.
Since JDK 1.7, when a java.lang.invoke.MethodHandle resolves to an operation that triggers initialization.
Class Unloading Timing
Unloading depends on the garbage‑collection algorithm. In CMS, a class can be unloaded when Metaspace is full (triggering a full GC) or via a concurrent collection similar to CMS. A class is eligible for unloading when:
All instances of the class have been reclaimed.
The class loader that loaded the class has been reclaimed.
The java.lang.Class object is no longer referenced anywhere, preventing reflective access.
Role of Class Loaders
Class loaders not only load bytecode into memory but also define a class's uniqueness by providing a separate namespace for each loader.
Even if two loaders load the same bytecode, the resulting classes are distinct. The following example demonstrates this:
The custom loader myLoader loads jvm.ClassLoaderTest as a different class from the one loaded by the application class loader, illustrating that class loaders provide isolation, enable unique class identification, and support middleware such as Tomcat.
Summary
The article has addressed the first two interview questions about JVM class loading and will continue with deeper topics in subsequent posts.
References
https://jrebel.com/rebellabs/do-you-really-get-java-classloaders/
https://stackoverflow.com/questions/2424604/what-is-a-java-classloader
https://docs.oracle.com/javase/9/docs/api/java/lang/ClassLoader.html
https://www.ibm.com/developerworks/cn/java/j-lo-classloader/index.html
https://blogs.oracle.com/sundararajan/understanding-java-class-loading
《深入理解Java虚拟机》
《揭秘Java虚拟机》
《Java性能权威指南》
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
