JVM-ClassLoader
Class Loader Subsystem ensures that classes are loaded safely, lazily, and hierarchically, enabling Java’s security, modularity, and runtime flexibility.
- Hardware Based/System Based Virtual Machine
- VMWare
- VirtualBox
- Cloud Computing (AWS EC2, Azure VM)
- Application Based/Process Based Vitual Machine
- JVM for Java
- .NET CLR for Dot Net
- PVM for Perl
Why we need JVM
JVM is responsible for making Java program Platform Independent,
We write a Java Code (.java) --then--> compiler (javac) --compile it and create --> ByteCode (.class) --then--> Java Interpreter convert into machine code
How JAVA Runs the Application
1️⃣ Class Loader Subsystem
2️⃣ Runtime Data Areas (Memory)
3️⃣ Execution Engine
4️⃣ Native Interface & Libraries
Class Loader Subsystem
- Loading
- Linking
- Initialization
What Loading does
JVM locates and reads a .class files and create an in-memory representation of that class.
When JVM sees a reference like:
new User();
Then:
- It finds the fully qualified class name
- Asks a class loader to load it.
- Reads bytecodes from:
- File System
- JAR
- Network (rare)
- Stores class metadata into MetaSpace
Types of Class Loader
It follows parent delegation model:
-
Bootstrap or Priomordial → serves as the parent of all the other ClassLoader instances and it is for loading JDK internal classes; eg. java.lang.*, java.util.*
-
Extension or Platform (from Java9) → child of the bootstrap class loader and takes care of loading the standard core java classes; eg. java.sql, java.xml
-
Application or System → child of the extension class loader and load your application-level classes;
new MyClass()is used- A static method is called
- A static field is accessed
- The class is explicitly loaded using:
- Class.forName("MyClass");
.class not found- Class was present during compile
- Missing at runtime
How do we manage / control this?
Developer actions
- Correct classpath
- Dependency management (Maven/Gradle)
- Avoid circular dependencies
What Linking does
Linking has 3 sub-steps
Verify
What happens?
JVM checks:
- Binary representation is structurally correct
- .class file is generated by valid compiler
- .class file is formatted properly
- Illegal memory access
WHY?
- Prevent malicious or corrupted bytecode
- Ensure JVM safety
Prepare
What happens?
- Static variable or Class leve variables are allocated memory
- Default values are assigned to the variables
Resolution - Optional
What happens?
- Symbolic references replaced with direct references
- Method calls linked to actual implementations
- During linking
- Or lazily at runtime
What Initialization does
- In this phase all allocated static variables are assigned with actual values defined in code.
- Class variables are assigned with their original values
- Static block execution in top to bottom or child to parent manner
WHY Initialization Is Separate
- Allows lazy class loading
- Improves startup performance
- Avoids unnecessary execution
Memory Interaction Summary
What Happens If the Class Loader Breaks?
Why JVM creates at Runtime not at System Boot?
- JVM is just another user-space process like chrome, tomcat, mysql therefore JVM start only when Java Command is executed
- JVM is application-specific and each application may require heap size, GC selection and JVM flags and it will be different application to application, so if it will create at Syatem boot then how it will identify which heap size require which GC will work.
- Multiple JVM can runs simulatenously for ex: 10 microservice we have each its own JVM, each with isolated memory and GC, If JVM was boot time then only one JVM possible, no isolation and one JVM crash bring down everything.

Comments
Post a Comment