Working with JVM Internals
1. Understanding JVM Architecture
| Component | Role |
|---|---|
| Class Loader | Load .class files |
| Runtime data areas | Heap, stacks, method area |
| Execution engine | Interpreter + JIT |
| Native interface | JNI for native code |
2. Understanding Class Loading
| Phase | Detail |
|---|---|
| Loading | Read .class bytes |
| Linking | Verify, prepare, resolve |
| Initialization | Run <clinit> (static init) |
3. Working with Class Loaders
| Loader | Loads |
|---|---|
| Bootstrap | java.base, etc. |
| Platform | JDK platform modules |
| App (system) | Application classes |
| Custom | Plugins, OSGi, app servers |
4. Understanding Bytecode
| Aspect | Detail |
|---|---|
| Format | Stack-based instructions |
| View | javap -c ClassName |
| Common ops | iload, istore, invokevirtual, invokespecial, invokestatic, invokedynamic |
5. Using JIT Compilation
| Tier | Detail |
|---|---|
| Interpreter | Initial execution |
| C1 | Quick optimizations, profiling |
| C2 | Aggressive optimizations |
| Tiered | Default — both C1 and C2 |
| Graal JIT | Alternative compiler (experimental) |
6. Understanding Method Area
| Aspect | Detail |
|---|---|
| Implementation | Metaspace (off-heap, since Java 8) |
| Stores | Class metadata, method info, runtime constant pool |
7. Understanding Runtime Constant Pool
| Stores | Detail |
|---|---|
| Symbolic refs | Class/method/field names |
| String literals | Interned |
| Constants | Numeric, boolean |
8. Working with Native Methods
| Aspect | Detail |
|---|---|
| Declaration | public native void foo(); |
| Loading | System.loadLibrary("name") |
| Modern alternative | Foreign Function & Memory API (Java 22+) |
9. Using JNI
| Aspect | Detail |
|---|---|
| Generate header | javac -h . |
| Implement | C/C++ functions following JNI naming |
| Cost | Boundary crossings are expensive |
10. Understanding Foreign Function and Memory API JAVA 22+
Example: FFM
Linker linker = Linker.nativeLinker();
SymbolLookup stdlib = linker.defaultLookup();
MethodHandle strlen = linker.downcallHandle(
stdlib.find("strlen").orElseThrow(),
FunctionDescriptor.of(ValueLayout.JAVA_LONG, ValueLayout.ADDRESS));
try (Arena arena = Arena.ofConfined()) {
MemorySegment cString = arena.allocateUtf8String("Hello");
long len = (long) strlen.invoke(cString);
}
| Component | Role |
|---|---|
Linker | Bridge to native ABI |
SymbolLookup | Find native symbols |
MemorySegment | Off-heap memory |
Arena | Memory lifetime management |
11. Using JVM Flags
| Flag Type | Detail |
|---|---|
-X | Non-standard (e.g., -Xmx) |
-XX: | Advanced/internal |
-XX:+/-Flag | Boolean |
-XX:Flag=value | Numeric/string |
| View all | java -XX:+PrintFlagsFinal |
12. Understanding Compilation Tiers
| Tier | Description |
|---|---|
| 0 | Interpreted |
| 1-3 | C1 compiled (varying profiling) |
| 4 | C2 (highly optimized) |
13. Using Class Data Sharing (CDS)
| Aspect | Detail |
|---|---|
| Purpose | Faster startup via shared class metadata |
| App CDS | -XX:ArchiveClassesAtExit=app.jsa |
| Use archive | -XX:SharedArchiveFile=app.jsa |