jvm internals douglas q. hawkins. jvm internals bytecode garbage collection optimizations compile...

24
JVM Internals JVM Internals Douglas Q. Hawkins Douglas Q. Hawkins

Upload: natalie-jackson

Post on 26-Mar-2015

228 views

Category:

Documents


3 download

TRANSCRIPT

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

JVM InternalsJVM InternalsDouglas Q. HawkinsDouglas Q. Hawkins

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

JVM InternalsJVM Internals

BytecodeBytecode

Garbage CollectionGarbage Collection

OptimizationsOptimizations

Compile TimeCompile Time

Run TimeRun Time

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

Java BytecodeJava Bytecode

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

Java BytecodeJava Bytecode

Stack BasedStack Based

Local Variable SpaceLocal Variable Space

Local VariablesLocal Variables

Operand StackOperand Stack

3377 ++1010

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

Operation TypesOperation Types

Load and StoreLoad and Store

Arithmetic and LogicArithmetic and Logic

Type ConversionType Conversion

Control TransferControl Transfer

Object Creation and ManipulationObject Creation and Manipulation

Operand StackOperand Stack

Method InvocationMethod Invocation

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

DemoDemo

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

Garbage CollectionGarbage Collection

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

Garbage CollectionGarbage Collection

Generational Garbage CollectionGenerational Garbage Collection

Segmented into Young, Old, and Permanent Segmented into Young, Old, and Permanent GenerationsGenerations

Types of CollectorsTypes of Collectors

Parallel - across multiple threadsParallel - across multiple threads

Concurrent - while program runsConcurrent - while program runs

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

DemoDemo

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

Garbage Collection Garbage Collection PatternPattern

MinorMinor

MajorMajor

Major Again - for objects with finalizeMajor Again - for objects with finalize

Soft ReferencesSoft References

MajorMajor

Major Again - for objects with finalizeMajor Again - for objects with finalize

Throw OutOfMemoryErrorThrow OutOfMemoryError

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

OptimizationsOptimizations

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

OptimizationsOptimizations

Just In Time CompilationJust In Time Compilation

Purely InterpretedPurely Interpreted

Ahead of Time CompilationAhead of Time Compilation

Almost No Compile Time Almost No Compile Time OptimizationOptimization

Most Optimizations are RuntimeMost Optimizations are Runtime

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

Compile Time DemoCompile Time Demo

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

Is This Optimized?Is This Optimized?double sumU = 0, sumV = 0;double sumU = 0, sumV = 0;for ( int i = 0; i < 100; ++i ) {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...?How many...?

Loop IterationsLoop Iterations

Heap AllocationsHeap Allocations

Method InvocationsMethod Invocations

Lock OperationsLock Operations

100100

100100

200200

100100

00

00

00

00

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

Common Sub-Common Sub-Expression EliminationExpression Eliminationint x = int x = a + ba + b;;int y = int y = a + ba + b;;

int int tmptmp = = a + ba + b;;int x = int x = tmptmp;;int y = int y = tmptmp;;

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

Array Bounds Check Array Bounds Check EliminationEliminationint[] nums = ...int[] nums = ...for ( int i = 0; i < nums.length; ++i ) {for ( int i = 0; i < nums.length; ++i ) {System.out.println( “nums[“ + i + “]=” + nums[ System.out.println( “nums[“ + i + “]=” + nums[ i ] );i ] );}}

int[] nums = ...int[] nums = ...for ( int i = 0; i < nums.length; ++i ) {for ( int i = 0; i < nums.length; ++i ) { if ( i < 0 || i >= nums.length ) {if ( i < 0 || i >= nums.length ) { throw new throw new ArrayIndexOutOfBoundsException();ArrayIndexOutOfBoundsException(); }}System.out.println( “nums[“ + i + “]=” + nums[ System.out.println( “nums[“ + i + “]=” + nums[ i ] );i ] );}}

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

Loop Invariant HoistingLoop Invariant Hoisting

for ( int i = 0; i < for ( int i = 0; i < nums.lengthnums.length; ++i ) {; ++i ) {......}}

int int lengthlength = = nums.lengthnums.length;;for ( int i = 0; i < for ( int i = 0; i < lengthlength; ++i ) {; ++i ) {......}}

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

Loop UnrollingLoop Unrolling

int sum = 0;int sum = 0;for ( int i = 0; i < 10; ++i ) {for ( int i = 0; i < 10; ++i ) { sum += i;sum += i;}}

int sum = 0;int sum = 0;sum += 1;sum += 1;......sum += 9;sum += 9;

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

Method InliningMethod InliningVector vector = ...Vector vector = ...double magnitude = double magnitude = vector.magnitudevector.magnitude();();

Vector vector = ...Vector vector = ...double magnitude = double magnitude = Math.sqrt( Math.sqrt( vector.u*vector.u + vector.v*vector.v );vector.u*vector.u + vector.v*vector.v );

Vector vector = ...Vector vector = ...double magnitude;double magnitude;if ( vector instance of Vector2D ) {if ( vector instance of Vector2D ) { magnitude = Math.sqrt(magnitude = Math.sqrt(vector.u*vector.u + vector.v*vector.v );vector.u*vector.u + vector.v*vector.v );} else {} else { magnitude = vector.magnitude();magnitude = vector.magnitude();}}

staticstatic

finalfinal

privateprivate

virtualvirtual

reflectivereflective

dynamicdynamic

alwaysalways

alwaysalways

alwaysalways

oftenoften

sometimessometimes

oftenoften

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

Lock CoarseningLock CoarseningStringBuffer buffer = ...StringBuffer buffer = ...buffer.buffer.appendappend( “Hello” );( “Hello” );buffer.buffer.appendappend( name );( name );buffer.buffer.appendappend( “\n” );( “\n” );StringBuffer buffer = ...StringBuffer buffer = ...locklock( buffer ); buffer.append( “Hello” ); ( buffer ); buffer.append( “Hello” ); unlockunlock( buffer );( buffer );locklock( buffer ); buffer.append( name ); ( buffer ); buffer.append( name ); unlockunlock( buffer );( buffer );locklock( buffer ); buffer.append( “\n” ); ( buffer ); buffer.append( “\n” ); unlockunlock( buffer );( buffer );StringBuffer buffer = ...StringBuffer buffer = ...locklock( buffer ); ( buffer ); buffer.append( “Hello” );buffer.append( “Hello” );buffer.append( name );buffer.append( name );buffer.append( “\n” );buffer.append( “\n” );unlockunlock( buffer );( buffer );

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

Other Lock Other Lock OptimizationsOptimizations

Biased LockingBiased Locking

Adaptive Locking - Thread sleep vs. Spin lockAdaptive Locking - Thread sleep vs. Spin lock

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

Escape AnalysisEscape AnalysisPoint p1 = new Point(x1, y1), p2 = new Point p1 = new Point(x1, y1), p2 = new Point(x2, y2);Point(x2, y2);

synchronized ( p2 ) {synchronized ( p2 ) { double dx = p1.getX() - p2.getX();double dx = p1.getX() - p2.getX();

synchronized ( p1 ) {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 = x1 - x2;double dx = y1 - y2;double dx = y1 - y2;double distance = Math.sqrt( dx*dx + dy*dy );double distance = Math.sqrt( dx*dx + dy*dy );

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

Run Time DemoRun Time Demo

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

ResourcesResources

Brian GoetzBrian Goetz

Developer Works ArticlesDeveloper Works Articles

Tony PrintezisTony Printezis

Garbage Collection in the Java HotSpot Virtual Machine - Garbage Collection in the Java HotSpot Virtual Machine - http://www.devx.com/Java/Article/21977

Java Specialist Newsletter - Java Specialist Newsletter - http://www.javaspecialists.euhttp://www.javaspecialists.eu//

http://java.sun.com/javase/6/docs/technotes/guides/vm/cms-6.htmlhttp://java.sun.com/javase/6/docs/technotes/guides/vm/cms-6.html

http://java.sun.com/docs/hotspot/gc1.4.2/faq.htmlhttp://java.sun.com/docs/hotspot/gc1.4.2/faq.html

http://www.fasterj.com/articles/G1.htmlhttp://www.fasterj.com/articles/G1.html

http://www.informit.com/guides/content.aspx?g=java&seqNum=27http://www.informit.com/guides/content.aspx?g=java&seqNum=27