JVM Internals Douglas Q Hawkins JVM Internals Bytecode
JVM Internals Douglas Q. Hawkins
JVM Internals Bytecode Garbage Collection Optimizations Compile Time Run Time
Java Bytecode
Java Bytecode Local Variables Stack Based Local Variable Space Operand Stack 7 3 + 10
Operation Types Load and Store Arithmetic and Logic Type Conversion Control Transfer Object Creation and Manipulation Operand Stack Method Invocation
Demo
Garbage Collection
Garbage Collection Generational Garbage Collection Segmented into Young, Old, and Permanent Generations Types of Collectors Parallel - across multiple threads Concurrent - while program runs
Demo
Garbage Collection Pattern Minor Major Again - for objects with finalize Soft References Major Again - for objects with finalize Throw Out. Of. Memory. Error
Optimizations
Optimizations Just In Time Compilation Purely Interpreted Ahead of Time Compilation Almost No Compile Time Optimization Most Optimizations are Runtime
Compile Time Demo
Is This Optimized? double sum. U = 0, sum. V = 0; for ( int i = 0; i < 100; ++i ) { Vector 2 D vector = new Vector 2 D( i, i ); synchronized ( vector ) { sum. U += vector. get. U(); How many. . . ? sum. V += vector. get. V(); Loop Iterations } Heap Allocations } 100 0 Method Invocations 200 0 Lock Operations 100 0
Common Sub-Expression Elimination int x = a + b; int y = a + b; int tmp = a + b; int x = tmp; int y = tmp;
Array Bounds Check Elimination int[] nums =. . . for ( int i = 0; i < nums. length; ++i ) { System. out. println( “nums[“ + i + “]=” + nums[ i ] ); } int[] nums =. . . for ( int i = 0; i < nums. length; ++i ) { if ( i < 0 || i >= nums. length ) { throw new Array. Index. Out. Of. Bounds. Exception(); } System. out. println( “nums[“ + i + “]=” + nums[ i ] ); }
Loop Invariant Hoisting for ( int i = 0; i < nums. length; ++i ) {. . . } int length = nums. length; for ( int i = 0; i < length; ++i ) {. . . }
Loop Unrolling int sum = 0; for ( int i = 0; i < 10; ++i ) { sum += i; } int sum = 0; sum += 1; . . . sum += 9;
Method Inlining Vector vector =. . . double magnitude = vector. magnitude(); static always Vector vector =. . . final always double magnitude = Math. sqrt( always vector. u*vector. u + vector. v*vector. v ); private virtual often Vector vector =. . . reflective sometimes double magnitude; dynamic often if ( vector instance of Vector 2 D ) { magnitude = Math. sqrt( vector. u*vector. u + vector. v*vector. v ); } else { magnitude = vector. magnitude(); }
Lock Coarsening String. Buffer buffer =. . . buffer. append( “Hello” ); buffer. append( name ); buffer. append( “n” ); String. Buffer buffer =. . . lock( buffer ); buffer. append( “Hello” ); unlock( buffer ); buffer. append( name ); unlock( buffer ); buffer. append( “n” ); unlock( buffer ); String. Buffer buffer =. . . lock( buffer ); buffer. append( “Hello” ); buffer. append( name ); buffer. append( “n” ); unlock( buffer );
Other Lock Optimizations Biased Locking Adaptive Locking - Thread sleep vs. Spin lock
Escape Analysis Point p 1 = new Point(x 1, y 1), p 2 = new Point(x 2, y 2); synchronized ( p 1 ) { synchronized ( p 2 ) { double dx = p 1. get. X() - p 2. get. X(); double dy = p 1. get. Y() - p 2. get. Y(); double distance = Math. sqrt( dx*dx + dy*dy ); } } double dx = x 1 - x 2; double dx = y 1 - y 2; double distance = Math. sqrt( dx*dx + dy*dy );
Run Time Demo
Resources Brian Goetz Developer Works Articles Tony Printezis Garbage Collection in the Java Hot. Spot Virtual Machine http: //www. devx. com/Java/Article/21977 Java Specialist Newsletter - http: //www. javaspecialists. eu/ http: //java. sun. com/javase/6/docs/technotes/guides/vm/cms-6. html http: //java. sun. com/docs/hotspot/gc 1. 4. 2/faq. html http: //www. fasterj. com/articles/G 1. html http: //www. informit. com/guides/content. aspx? g=java&seq. Num=27
- Slides: 24