The document discusses key aspects of how the Java Virtual Machine (JVM) executes Java programs. It describes the launcher, class loading process, bytecode verification, class initialization, just-in-time compilation, threads, synchronization, memory management including garbage collection, and how exceptions and native methods are handled. Troubleshooting techniques like hs_err logs are also mentioned. The JVM performs complex optimizations to efficiently run Java programs.
4. Launcher
• java.exe – just a simple C program
1. Locate JRE
2. Select version
3. Parse arguments
4. JNI_CreateJavaVM
5. JNIEnv::FindClass
6. JNIEnv::GetStaticMethodID (main)
7. JNIEnv::CallStaticVoidMethod
3
5. Other launchers
• Differ only by Main Class
– javac com.sun.tools.javac.Main
– javadoc com.sun.tools.javadoc.Main
– javah com.sun.tools.javah.Main
– jconsole sun.tools.jconsole.JConsole
– jmap sun.tools.jmap.JMap
– …
• Even -version calls Java
– sun.misc.Version.print()
4
6. Class loading
• Class loading != Class initialization
• ClassLoader prepares byte[] definition
• Real loading is done inside VM
– ClassLoader.defineClass0
– Parses, verifies and creates internal VM structures
• Unreferenced classes may be unloaded
– -XX:+CMSClassUnloadingEnabled
5
7. Class Metadata
• Constant Pool, Interfaces, Methods, Fields
• Exceptions, Annotations, vtables, itables
• Java 8: PermGen Metaspace
– java.lang.OutOfMemoryError: PermGen space
• Field reordering
– -XX:+CompactFields, -XX:FieldsAllocationStyle=1
6
8. Class data sharing
• Snapshot of commonly used classes
– jre/lib/classlist
• Mapped into memory directly from disk
– classes.jsa
• Shared between multiple JVM instances
• Improves start-up time, reduces footprint
– -XX:+UseSharedSpaces
– -XX:+DumpSharedSpaces
7
24. Native methods
• System.loadLibrary()
• Lazy linking
• Expensive invocation
1. Create stack frame
2. Set up arguments according to native calling convention
3. Pass JNIEnv* and jclass
4. Lock / unlock if synchronized
5. Trace method entry / method exit
6. Check for safepoint
7. Check exceptions
23
25. JNI functions
• Executed in VM context
• Check for safepoint
• jobject == index in thread-local JNIHandles array
• Optional verification
– -XX:+CheckJNICalls
24
26. Exceptions
• Which code is faster?
for (;;) { try {
int index = getNextIndex(); for (;;) {
if (index >= arr.length) { int index = getNextIndex();
break; sum += arr[index];
} }
sum += array[index]; } catch (IndexOutOfBoundsException e) {
} // break
}
25
27. Exceptions
• Which code is faster?
for (;;) { try {
int index = getNextIndex(); for (;;) {
if (index >= arr.length) { int index = getNextIndex();
break; sum += arr[index];
} }
sum += array[index]; } catch (IndexOutOfBoundsException e) {
} // break
}
• try-catch is free (exception tables)
• throw is expensive
(find handler, unwind stack, release locks, build stack trace)
26
28. Reflection
• getDeclaredFields(), getDeclaredMethods()
– VM Internal structures Java representation
• Field getters and setters
– sun.misc.Unsafe
• Method.invoke()
1. Native implementation
2. Dynamic bytecode generation (up to 10x faster)
-Dsun.reflect.inflationThreshold=15
27
29. MethodAccessor example
• void Point.move(float x, float y, boolean relative);
class PointMove_MethodAccessor implements MethodAccessor {
public Object invoke(Object target, Object[] args) {
float x = ((Float) args[0]).floatValue();
float y = ((Float) args[1]).floatValue();
boolean relative = ((Boolean) args[2]).booleanValue();
try {
((Point) target).move(x, y, relative);
} catch (Throwable t) {
throw new InvocationTargetException(t);
}
return null;
}
}
28
30. Troubleshooting
• Signal handler (SIGSEGV, SIGILL, SIGFPE…)
• Not all SEGV are fatal
– Polling page
– Implicit NullPointerException, StackOverflowError
• hs_err.log
– Threads, Stack frames, Memory map
– Registers, Top of stack, Instructions
– -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly
29