Working with JVM Internals

1. Understanding JVM Architecture

ComponentRole
Class LoaderLoad .class files
Runtime data areasHeap, stacks, method area
Execution engineInterpreter + JIT
Native interfaceJNI for native code

2. Understanding Class Loading

PhaseDetail
LoadingRead .class bytes
LinkingVerify, prepare, resolve
InitializationRun <clinit> (static init)

3. Working with Class Loaders

LoaderLoads
Bootstrapjava.base, etc.
PlatformJDK platform modules
App (system)Application classes
CustomPlugins, OSGi, app servers

4. Understanding Bytecode

AspectDetail
FormatStack-based instructions
Viewjavap -c ClassName
Common opsiload, istore, invokevirtual, invokespecial, invokestatic, invokedynamic

5. Using JIT Compilation

TierDetail
InterpreterInitial execution
C1Quick optimizations, profiling
C2Aggressive optimizations
TieredDefault — both C1 and C2
Graal JITAlternative compiler (experimental)

6. Understanding Method Area

AspectDetail
ImplementationMetaspace (off-heap, since Java 8)
StoresClass metadata, method info, runtime constant pool

7. Understanding Runtime Constant Pool

StoresDetail
Symbolic refsClass/method/field names
String literalsInterned
ConstantsNumeric, boolean

8. Working with Native Methods

AspectDetail
Declarationpublic native void foo();
LoadingSystem.loadLibrary("name")
Modern alternativeForeign Function & Memory API (Java 22+)

9. Using JNI

AspectDetail
Generate headerjavac -h .
ImplementC/C++ functions following JNI naming
CostBoundary 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);
}
ComponentRole
LinkerBridge to native ABI
SymbolLookupFind native symbols
MemorySegmentOff-heap memory
ArenaMemory lifetime management

11. Using JVM Flags

Flag TypeDetail
-XNon-standard (e.g., -Xmx)
-XX:Advanced/internal
-XX:+/-FlagBoolean
-XX:Flag=valueNumeric/string
View alljava -XX:+PrintFlagsFinal

12. Understanding Compilation Tiers

TierDescription
0Interpreted
1-3C1 compiled (varying profiling)
4C2 (highly optimized)

13. Using Class Data Sharing (CDS)

AspectDetail
PurposeFaster startup via shared class metadata
App CDS-XX:ArchiveClassesAtExit=app.jsa
Use archive-XX:SharedArchiveFile=app.jsa