⚙️ JVM Internals: Key Areas
1. JIT Compiler (Just-In-Time)
Concept | Description |
---|---|
JIT | Compiles frequently-used bytecode into native machine code at runtime. |
HotSpot JVM | Uses adaptive optimization to compile “hot” code paths. |
C1 vs C2 | C1: Fast compile, less optimized (client). C2: Slower compile, highly optimized (server). |
Tiered Compilation | Combines C1 & C2 to balance startup and performance (default since Java 8). |
✅ Why it matters: JIT reduces interpretation overhead, boosts throughput, and adapts to real workloads.
🔹 JVM Execution Strategies
Strategy | Description |
---|---|
Interpretation | Bytecode is read and executed line-by-line |
JIT Compilation | Hot methods are compiled into native code |
🔹 How JIT Works
-
HotSpot JVM detects "hot methods" (frequently invoked)
-
Compiles them into native machine code
-
Uses profiling information (branch prediction, inlining, etc.)
-
Code is optimized during runtime (adaptive optimization)
🔹 Tiered Compilation
Level | Name | Description |
---|---|---|
0 | Interpreter | Interprets bytecode |
1 | C1 (Client) | Fast compilation (lightweight optimization) |
2 | C2 (Server) | Aggressive optimization, longer compile time |
🔹 JIT Techniques
-
Method inlining: Replace method calls with actual body
-
Loop unrolling: Optimize loop execution
-
Escape analysis: Allocate objects on stack (reduce GC pressure)
-
Dead code elimination
🔹 JIT Control Flags
-XX:+PrintInlining
2. Class Loading
ClassLoader Hierarchy
Java uses a parent delegation model for class loading:
-
Bootstrap ClassLoader
Loads JDK core classes (e.g.,java.lang.*
) -
Platform (Extension) ClassLoader
Loads classes from$JAVA_HOME/lib/ext
-
Application ClassLoader
Loads classes from the application classpath -
Custom ClassLoaders
Useful in frameworks (Spring, PF4J, OSGi, JBoss modules)
Concept | Description |
---|---|
ClassLoaders | Load .class files into JVM memory. |
Bootstrap | Loads core Java classes (java.* ). |
Extension | Loads classes from lib/ext . |
Application/System | Loads classes from your app’s classpath. |
Custom ClassLoaders | Used for modular systems, plugins, isolation (PF4J, OSGi). |
✅ Why it matters: Understanding class loading helps avoid
ClassNotFoundException
,ClassCastException
, memory leaks in containers.
🔹 Important Concepts
Term | Description |
---|---|
Parent Delegation | ClassLoader first delegates loading to parent |
Linking | Verifies and prepares bytecode before execution |
Loading | Finds and reads bytecode of class |
Initialization | Runs static initializers and assigns constants |
3. JVM Execution Pipeline
🚀 Performance Tuning Essentials
🔧 JVM Options
Option | Purpose |
---|---|
-Xms / -Xmx | Set initial and max heap size. |
-XX:+UseG1GC | Use G1 Garbage Collector (default in Java 17). |
-XX:+PrintGCDetails | Enable GC logs for analysis. |
-XX:+TieredCompilation | Enable both C1 and C2 compilers. |
-XX:+AlwaysPreTouch | Pre-touch memory pages for large heaps. |
-Xlog:gc*,safepoint,class+load=debug | Log class loading and safepoint info. |
📊 Profiling Tools
Tool | Use |
---|---|
JVisualVM | Real-time heap/CPU/thread monitoring. |
Java Flight Recorder (JFR) | Low-overhead JVM profiling (built-in Java 11+). |
async-profiler | Sampling-based low-level profiler. |
JMC (Java Mission Control) | GUI for JFR analysis. |
jcmd/jmap/jstack | CLI diagnostics for heap, threads, deadlocks. |
🔥 Hot Tips for Interviews
-
“JIT compiles frequently used code to native machine code—significantly improving execution speed over interpreted bytecode.”
-
“Custom ClassLoaders help isolate third-party dependencies and enable plugin systems.”
-
“We used JFR + G1 GC tuning to reduce GC pause time under high throughput.”
-
“Knowing the JVM call stack and safepoint behavior helps explain latency spikes and lock contention.”
✅ Pro Interview Format
“I optimized JVM performance by enabling Tiered Compilation and G1 GC, and tuned heap size via
-Xms/-Xmx
to prevent frequent GC. We also used JFR to identify hotspots and reduced method call overhead via JIT inlining. Understanding class loading helped us fix plugin isolation issues using custom class loaders.”
JVM Memory Areas
Area | Description |
---|---|
Heap | Object storage; GC-managed |
Stack | Per-thread call frames and primitive vars |
Metaspace | Class metadata storage |
Code Cache | Stores JIT-compiled machine code |
Direct Memory | Off-heap (e.g., NIO buffers) |
JVM Performance Tuning
🔹 Key Performance Goals
-
Minimize GC pause times
-
Minimize context switching
-
Maximize CPU utilization
-
Tune for latency or throughput based on use case
🔹 JVM Startup Tuning
🔹 GC Tuning
-
Use
G1GC
orZGC
for large heaps and low latency
🔹 JIT/Code Cache Tuning
🔹 Class Loading
🔬 6. Monitoring & Diagnostics
🔹 Tools
Tool | Purpose |
---|---|
jcmd | Real-time diagnostics (heap, threads, JIT stats) |
jmap | Heap dump |
jstack | Thread dump |
JFR (Java Flight Recorder) | Deep performance diagnostics |
JVisualVM / Mission Control | Live monitoring, CPU/Heap profiling |
No comments:
Post a Comment