JVM Internals - Garbage Collection & Runtime Optimizations

55
JVM Internals Douglas Q. Hawkins Sunday, August 15, 2010

description

Oct 2009 Meetup Presentation - focusing on Garbage Collection and Run Time Optimizations

Transcript of JVM Internals - Garbage Collection & Runtime Optimizations

Page 1: JVM Internals - Garbage Collection & Runtime Optimizations

JVM InternalsDouglas Q. Hawkins

Sunday, August 15, 2010

Page 2: JVM Internals - Garbage Collection & Runtime Optimizations

JVM Internals

Bytecode

Garbage Collection

Optimizations

Compile Time

Run Time

Sunday, August 15, 2010

Page 3: JVM Internals - Garbage Collection & Runtime Optimizations

Java Bytecode

Sunday, August 15, 2010

Page 4: JVM Internals - Garbage Collection & Runtime Optimizations

Java Bytecode

Stack Based

Local Variable Space

Local Variables

Operand Stack

Sunday, August 15, 2010

Page 5: JVM Internals - Garbage Collection & Runtime Optimizations

Java Bytecode

Stack Based

Local Variable Space

Local Variables

Operand Stack

3

Sunday, August 15, 2010

Page 6: JVM Internals - Garbage Collection & Runtime Optimizations

Java Bytecode

Stack Based

Local Variable Space

Local Variables

Operand Stack

37

Sunday, August 15, 2010

Page 7: JVM Internals - Garbage Collection & Runtime Optimizations

Java Bytecode

Stack Based

Local Variable Space

Local Variables

Operand Stack

37 +

Sunday, August 15, 2010

Page 8: JVM Internals - Garbage Collection & Runtime Optimizations

Java Bytecode

Stack Based

Local Variable Space

Local Variables

Operand Stack

3 7+

Sunday, August 15, 2010

Page 9: JVM Internals - Garbage Collection & Runtime Optimizations

Java Bytecode

Stack Based

Local Variable Space

Local Variables

Operand Stack

10

Sunday, August 15, 2010

Page 10: JVM Internals - Garbage Collection & Runtime Optimizations

Operation Types

Load and Store

Arithmetic and Logic

Type Conversion

Control Transfer

Object Creation and Manipulation

Operand Stack

Method Invocation

Sunday, August 15, 2010

Page 11: JVM Internals - Garbage Collection & Runtime Optimizations

Demo

Sunday, August 15, 2010

Page 12: JVM Internals - Garbage Collection & Runtime Optimizations

Garbage Collection

Sunday, August 15, 2010

Page 13: JVM Internals - Garbage Collection & Runtime Optimizations

Garbage Collection

Generational Garbage Collection

Segmented into Young, Old, and Permanent Generations

Types of Collectors

Parallel - across multiple threads

Concurrent - while program runs

Sunday, August 15, 2010

Page 14: JVM Internals - Garbage Collection & Runtime Optimizations

Generational Garbage Collection

Young Generation

Eden

SurvivorSpaces

Old Generation

Tenured

Sunday, August 15, 2010

Page 15: JVM Internals - Garbage Collection & Runtime Optimizations

Generational Garbage Collection

Young Generation

Eden

SurvivorSpaces

Old Generation

Tenured

A B

Sunday, August 15, 2010

Page 16: JVM Internals - Garbage Collection & Runtime Optimizations

Generational Garbage Collection

Young Generation

Eden

SurvivorSpaces

Old Generation

Tenured

A B C D

Sunday, August 15, 2010

Page 17: JVM Internals - Garbage Collection & Runtime Optimizations

Generational Garbage Collection

Young Generation

Eden

SurvivorSpaces

Old Generation

Tenured

A B C D E F G

Sunday, August 15, 2010

Page 18: JVM Internals - Garbage Collection & Runtime Optimizations

Generational Garbage Collection

Young Generation

Eden

SurvivorSpaces

Old Generation

Tenured

A B C D E F G

Sunday, August 15, 2010

Page 19: JVM Internals - Garbage Collection & Runtime Optimizations

Generational Garbage Collection

Young Generation

Eden

SurvivorSpaces

Old Generation

Tenured

A

B

C D

E F

G

Sunday, August 15, 2010

Page 20: JVM Internals - Garbage Collection & Runtime Optimizations

Generational Garbage Collection

Young Generation

Eden

SurvivorSpaces

Old Generation

Tenured

B E F

Sunday, August 15, 2010

Page 21: JVM Internals - Garbage Collection & Runtime Optimizations

Generational Garbage Collection

Young Generation

Eden

SurvivorSpaces

Old Generation

Tenured

B E F

H I J K L M N

Sunday, August 15, 2010

Page 22: JVM Internals - Garbage Collection & Runtime Optimizations

Generational Garbage Collection

Young Generation

Eden

SurvivorSpaces

Old Generation

Tenured

B E F

H I J K L M N

Sunday, August 15, 2010

Page 23: JVM Internals - Garbage Collection & Runtime Optimizations

Generational Garbage Collection

Young Generation

Eden

SurvivorSpaces

Old Generation

Tenured

B EF

H I

J

K L M N

Sunday, August 15, 2010

Page 24: JVM Internals - Garbage Collection & Runtime Optimizations

Generational Garbage Collection

Young Generation

Eden

SurvivorSpaces

Old Generation

Tenured

B E J

Sunday, August 15, 2010

Page 25: JVM Internals - Garbage Collection & Runtime Optimizations

Generational Garbage Collection

Young Generation

Eden

SurvivorSpaces

Old Generation

Tenured B E J

Sunday, August 15, 2010

Page 26: JVM Internals - Garbage Collection & Runtime Optimizations

Demo

Sunday, August 15, 2010

Page 27: JVM Internals - Garbage Collection & Runtime Optimizations

Garbage Collection Pattern

Minor

Major

Major Again - for objects with finalize

Soft References

Major

Major Again - for objects with finalize

Throw OutOfMemoryError

Sunday, August 15, 2010

Page 28: JVM Internals - Garbage Collection & Runtime Optimizations

Optimizations

Sunday, August 15, 2010

Page 29: JVM Internals - Garbage Collection & Runtime Optimizations

Optimizations

Just In Time Compilation

Purely Interpreted

Ahead of Time Compilation

Almost No Compile Time Optimization

Most Optimizations are Runtime

Sunday, August 15, 2010

Page 30: JVM Internals - Garbage Collection & Runtime Optimizations

Compile Time Demo

Sunday, August 15, 2010

Page 31: JVM Internals - Garbage Collection & Runtime Optimizations

Is This Optimized?double sumU = 0, sumV = 0;for ( int i = 0; i < 100; ++i ) { Vector2D vector = new Vector2D( i, i ); synchronized ( vector ) { sumU += vector.getU(); sumV += vector.getV(); }}

Sunday, August 15, 2010

Page 32: JVM Internals - Garbage Collection & Runtime Optimizations

Is This Optimized?double sumU = 0, sumV = 0;for ( int i = 0; i < 100; ++i ) { Vector2D vector = new Vector2D( i, i ); synchronized ( vector ) { sumU += vector.getU(); sumV += vector.getV(); }}

How many...?

Loop Iterations

Heap Allocations

Method Invocations

Lock Operations

Sunday, August 15, 2010

Page 33: JVM Internals - Garbage Collection & Runtime Optimizations

Is This Optimized?double sumU = 0, sumV = 0;for ( int i = 0; i < 100; ++i ) { Vector2D vector = new Vector2D( i, i ); synchronized ( vector ) { sumU += vector.getU(); sumV += vector.getV(); }}

How many...?

Loop Iterations

Heap Allocations

Method Invocations

Lock Operations

100

100

200

100

Sunday, August 15, 2010

Page 34: JVM Internals - Garbage Collection & Runtime Optimizations

Is This Optimized?double sumU = 0, sumV = 0;for ( int i = 0; i < 100; ++i ) { Vector2D vector = new Vector2D( i, i ); synchronized ( vector ) { sumU += vector.getU(); sumV += vector.getV(); }}

How many...?

Loop Iterations

Heap Allocations

Method Invocations

Lock Operations

0

0

0

0

Sunday, August 15, 2010

Page 35: JVM Internals - Garbage Collection & Runtime Optimizations

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

Sunday, August 15, 2010

Page 36: JVM Internals - Garbage Collection & Runtime Optimizations

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

int tmp = a + b;int x = tmp;int y = tmp;

Sunday, August 15, 2010

Page 37: JVM Internals - Garbage Collection & Runtime Optimizations

Array Bounds Check Eliminationint[] nums = ...for ( int i = 0; i < nums.length; ++i ) {

System.out.println( “nums[“ + i + “]=” + nums[ i ] );}

Sunday, August 15, 2010

Page 38: JVM Internals - Garbage Collection & Runtime Optimizations

Array Bounds Check Eliminationint[] 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 ArrayIndexOutOfBoundsException(); }

System.out.println( “nums[“ + i + “]=” + nums[ i ] );}

Sunday, August 15, 2010

Page 39: JVM Internals - Garbage Collection & Runtime Optimizations

Loop Invariant Hoisting

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

Sunday, August 15, 2010

Page 40: JVM Internals - Garbage Collection & Runtime Optimizations

Loop Invariant Hoisting

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

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

Sunday, August 15, 2010

Page 41: JVM Internals - Garbage Collection & Runtime Optimizations

Loop Unrolling

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

Sunday, August 15, 2010

Page 42: JVM Internals - Garbage Collection & Runtime Optimizations

Loop Unrolling

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

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

Sunday, August 15, 2010

Page 43: JVM Internals - Garbage Collection & Runtime Optimizations

Method InliningVector vector = ...double magnitude = vector.magnitude();

Sunday, August 15, 2010

Page 44: JVM Internals - Garbage Collection & Runtime Optimizations

Method InliningVector vector = ...double magnitude = vector.magnitude();

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

Sunday, August 15, 2010

Page 45: JVM Internals - Garbage Collection & Runtime Optimizations

Method InliningVector vector = ...double magnitude = vector.magnitude();

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

Vector vector = ...double magnitude;if ( vector instance of Vector2D ) { magnitude = Math.sqrt(

vector.u*vector.u + vector.v*vector.v );} else { magnitude = vector.magnitude();}

Sunday, August 15, 2010

Page 46: JVM Internals - Garbage Collection & Runtime Optimizations

Method InliningVector vector = ...double magnitude = vector.magnitude();

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

Vector vector = ...double magnitude;if ( vector instance of Vector2D ) { magnitude = Math.sqrt(

vector.u*vector.u + vector.v*vector.v );} else { magnitude = vector.magnitude();}

static

final

private

virtual

reflective

dynamic

always

always

always

often

sometimes

often

Sunday, August 15, 2010

Page 47: JVM Internals - Garbage Collection & Runtime Optimizations

Lock CoarseningStringBuffer buffer = ...buffer.append( “Hello” );buffer.append( name );buffer.append( “\n” );

Sunday, August 15, 2010

Page 48: JVM Internals - Garbage Collection & Runtime Optimizations

Lock CoarseningStringBuffer 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 );

Sunday, August 15, 2010

Page 49: JVM Internals - Garbage Collection & Runtime Optimizations

Lock CoarseningStringBuffer 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 );

Sunday, August 15, 2010

Page 50: JVM Internals - Garbage Collection & Runtime Optimizations

Other Lock Optimizations

Biased Locking

Adaptive Locking - Thread sleep vs. Spin lock

Sunday, August 15, 2010

Page 51: JVM Internals - Garbage Collection & Runtime Optimizations

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

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

synchronized ( p1 ) {

double dy = p1.getY() - p2.getY();

}}

double distance = Math.sqrt( dx*dx + dy*dy );

Sunday, August 15, 2010

Page 52: JVM Internals - Garbage Collection & Runtime Optimizations

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

double dx = p1.getX() - p2.getX(); double dy = p1.getY() - p2.getY(); double distance = Math.sqrt( dx*dx + dy*dy );

Sunday, August 15, 2010

Page 53: JVM Internals - Garbage Collection & Runtime Optimizations

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

double dx = p1.getX() - p2.getX(); double dy = p1.getY() - p2.getY(); double distance = Math.sqrt( dx*dx + dy*dy );

double dx = x1 - x2;double dx = y1 - y2;double distance = Math.sqrt( dx*dx + dy*dy );

Sunday, August 15, 2010

Page 54: JVM Internals - Garbage Collection & Runtime Optimizations

Run Time Demo

Sunday, August 15, 2010

Page 55: JVM Internals - Garbage Collection & Runtime Optimizations

Resources

Brian Goetz

Developer Works Articles

Tony Printezis

Garbage Collection in the Java HotSpot 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/gc1.4.2/faq.html

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

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

Sunday, August 15, 2010