Presentation is loading. Please wait.

Presentation is loading. Please wait.

JVM Internals Douglas Q. Hawkins. JVM Internals Bytecode Garbage Collection Optimizations Compile Time Run Time.

Similar presentations


Presentation on theme: "JVM Internals Douglas Q. Hawkins. JVM Internals Bytecode Garbage Collection Optimizations Compile Time Run Time."— Presentation transcript:

1 JVM Internals Douglas Q. Hawkins

2 JVM Internals Bytecode Garbage Collection Optimizations Compile Time Run Time

3 Java Bytecode

4 Stack Based Local Variable Space Local Variables Operand Stack

5 Operation Types Load and Store Arithmetic and Logic Type Conversion Control Transfer Object Creation and Manipulation Operand Stack Method Invocation

6 Demo

7 Garbage Collection

8 Generational Garbage Collection Segmented into Young, Old, and Permanent Generations Types of Collectors Parallel - across multiple threads Concurrent - while program runs

9 Demo

10 Garbage Collection Pattern MinorMajor Major Again - for objects with finalize Soft References Major Major Again - for objects with finalize Throw OutOfMemoryError

11 Optimizations

12 Optimizations Just In Time Compilation Purely Interpreted Ahead of Time Compilation Almost No Compile Time Optimization Most Optimizations are Runtime

13 Compile Time Demo

14 Is This Optimized? double sumU = 0, sumV = 0; for ( int i = 0; i < 100; ++i ) { Vector2D vector = new Vector2D( i, i ); Vector2D vector = new Vector2D( i, i ); synchronized ( vector ) { synchronized ( vector ) { sumU += vector.getU(); sumU += vector.getU(); sumV += vector.getV(); sumV += vector.getV(); }} How many...? Loop Iterations Heap Allocations Method Invocations Lock Operations

15 Common Sub-Expression Elimination int x = a + b; int y = a + b; int tmp = a + b; int x = tmp; int y = tmp;

16 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 = nums.length ) { if ( i = nums.length ) { throw new ArrayIndexOutOfBoundsException(); throw new ArrayIndexOutOfBoundsException(); } System.out.println( nums[ + i + ]= + nums[ i ] ); }

17 Loop Invariant Hoisting for ( int i = 0; i < nums.length; ++i ) {...} int length = nums.length; for ( int i = 0; i < length; ++i ) {...}

18 Loop Unrolling int sum = 0; for ( int i = 0; i < 10; ++i ) { sum += i; sum += i;} int sum = 0; sum += 1;... sum += 9;

19 Method Inlining Vector vector =... double magnitude = vector.magnitude(); Vector vector =... double magnitude = Math.sqrt( vector.u*vector.u + vector.v*vector.v ); vector.u*vector.u + vector.v*vector.v ); Vector vector =... double magnitude; if ( vector instance of Vector2D ) { magnitude = Math.sqrt( magnitude = Math.sqrt( vector.u*vector.u + vector.v*vector.v ); } else { magnitude = vector.magnitude(); magnitude = vector.magnitude();} static final private virtual reflective dynamic always always always often sometimes often

20 Lock Coarsening StringBuffer buffer =... buffer.append( Hello ); buffer.append( name ); buffer.append( \n ); StringBuffer buffer =... lock( buffer ); buffer.append( Hello ); unlock( buffer ); lock( buffer ); buffer.append( name ); unlock( buffer ); lock( buffer ); buffer.append( \n ); unlock( buffer ); StringBuffer buffer =... lock( buffer ); buffer.append( Hello ); buffer.append( name ); buffer.append( \n ); unlock( buffer );

21 Other Lock Optimizations Biased Locking Adaptive Locking - Thread sleep vs. Spin lock

22 Escape Analysis Point p1 = new Point(x1, y1), p2 = new Point(x2, y2); synchronized ( p2 ) { synchronized ( p2 ) { double dx = p1.getX() - p2.getX(); double dx = p1.getX() - p2.getX(); synchronized ( p1 ) { double dy = p1.getY() - p2.getY(); double dy = p1.getY() - p2.getY(); } } double distance = Math.sqrt( dx*dx + dy*dy ); double distance = Math.sqrt( dx*dx + dy*dy ); double dx = x1 - x2; double dx = y1 - y2; double distance = Math.sqrt( dx*dx + dy*dy );

23 Run Time Demo

24 Resources Brian Goetz Developer Works Articles Tony Printezis Garbage Collection in the Java HotSpot Virtual Machine - Garbage Collection in the Java HotSpot Virtual Machine - Java Specialist Newsletter


Download ppt "JVM Internals Douglas Q. Hawkins. JVM Internals Bytecode Garbage Collection Optimizations Compile Time Run Time."

Similar presentations


Ads by Google