0% found this document useful (0 votes)
59 views

JVM Internals

The document discusses Java Virtual Machine (JVM) internals including bytecode, garbage collection, and optimizations. It describes how bytecode is stack-based and uses an operand stack and local variable space. It explains generational garbage collection with young and old generations. It also discusses just-in-time compilation and runtime optimizations like common subexpression elimination and array bounds check elimination.

Uploaded by

Amol Chikhalkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

JVM Internals

The document discusses Java Virtual Machine (JVM) internals including bytecode, garbage collection, and optimizations. It describes how bytecode is stack-based and uses an operand stack and local variable space. It explains generational garbage collection with young and old generations. It also discusses just-in-time compilation and runtime optimizations like common subexpression elimination and array bounds check elimination.

Uploaded by

Amol Chikhalkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

JVM Internals

Douglas Q. Hawkins

Sunday, August 15, 2010


JVM Internals

Bytecode
Garbage Collection
Optimizations
Compile Time
Run Time

Sunday, August 15, 2010


Java Bytecode

Sunday, August 15, 2010


Java Bytecode
Local Variables
Stack Based
Operand Stack
Local Variable Space

Sunday, August 15, 2010


Java Bytecode
Local Variables
Stack Based
Operand Stack
Local Variable Space

Sunday, August 15, 2010


Java Bytecode
Local Variables
Stack Based
Operand Stack
Local Variable Space

7
3

Sunday, August 15, 2010


Java Bytecode
Local Variables
Stack Based
Operand Stack
Local Variable Space

7 +
3

Sunday, August 15, 2010


Java Bytecode
Local Variables
Stack Based
Operand Stack
Local Variable Space

3+ 7

Sunday, August 15, 2010


Java Bytecode
Local Variables
Stack Based
Operand Stack
Local Variable Space

10

Sunday, August 15, 2010


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

Sunday, August 15, 2010


Demo

Sunday, August 15, 2010


Garbage Collection

Sunday, August 15, 2010


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


Generational Garbage
Collection
Young Generation

Eden

Survivor
Spaces
Old Generation
Tenured

Sunday, August 15, 2010


Generational Garbage
Collection
Young Generation

Eden A B

Survivor
Spaces
Old Generation
Tenured

Sunday, August 15, 2010


Generational Garbage
Collection
Young Generation

Eden A B C D

Survivor
Spaces
Old Generation
Tenured

Sunday, August 15, 2010


Generational Garbage
Collection
Young Generation

Eden A B C D E F G

Survivor
Spaces
Old Generation
Tenured

Sunday, August 15, 2010


Generational Garbage
Collection
Young Generation

Eden A B C D E F G

Survivor
Spaces
Old Generation
Tenured

Sunday, August 15, 2010


Generational Garbage
Collection
Young Generation

Eden A C D G

Survivor
B E F
Spaces
Old Generation
Tenured

Sunday, August 15, 2010


Generational Garbage
Collection
Young Generation

Eden

Survivor
B E F
Spaces
Old Generation
Tenured

Sunday, August 15, 2010


Generational Garbage
Collection
Young Generation

Eden H I J K L M N

Survivor
B E F
Spaces
Old Generation
Tenured

Sunday, August 15, 2010


Generational Garbage
Collection
Young Generation

Eden H I J K L M N

Survivor
B E F
Spaces
Old Generation
Tenured

Sunday, August 15, 2010


Generational Garbage
Collection
Young Generation

Eden H I K L M N

Survivor
F B E J
Spaces
Old Generation
Tenured

Sunday, August 15, 2010


Generational Garbage
Collection
Young Generation

Eden

Survivor
B E J
Spaces
Old Generation
Tenured

Sunday, August 15, 2010


Generational Garbage
Collection
Young Generation

Eden

Survivor
Spaces
Old Generation
Tenured B E J

Sunday, August 15, 2010


Demo

Sunday, August 15, 2010


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


Optimizations

Sunday, August 15, 2010


Optimizations
Just In Time Compilation
Purely Interpreted
Ahead of Time Compilation

Almost No Compile Time Optimization


Most Optimizations are Runtime

Sunday, August 15, 2010


Compile Time Demo

Sunday, August 15, 2010


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


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(); How many...?
sumV += vector.getV(); Loop Iterations
}
Heap Allocations
}
Method Invocations
Lock Operations

Sunday, August 15, 2010


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(); How many...?
sumV += vector.getV(); Loop Iterations 100
}
Heap Allocations 100
}
Method Invocations 200
Lock Operations 100

Sunday, August 15, 2010


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(); How many...?
sumV += vector.getV(); Loop Iterations 0
}
Heap Allocations 0
}
Method Invocations 0
Lock Operations 0

Sunday, August 15, 2010


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

Sunday, August 15, 2010


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

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

Sunday, August 15, 2010


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

Sunday, August 15, 2010


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 ArrayIndexOutOfBoundsException();
}
System.out.println( “nums[“ + i + “]=” + nums[ i ] );
}

Sunday, August 15, 2010


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

Sunday, August 15, 2010


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


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

Sunday, August 15, 2010


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


Method Inlining
Vector vector = ...
double magnitude = vector.magnitude();

Sunday, August 15, 2010


Method Inlining
Vector vector = ...
double magnitude = vector.magnitude();

Vector vector = ...


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

Sunday, August 15, 2010


Method Inlining
Vector 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
Method Inlining
Vector vector = ...
double magnitude = vector.magnitude();
static always
Vector vector = ...
final always
double magnitude = Math.sqrt(
vector.u*vector.u + vector.v*vector.v ); private always
virtual often
Vector vector = ...
reflective sometimes
double magnitude;
if ( vector instance of Vector2D ) { dynamic often
magnitude = Math.sqrt(
vector.u*vector.u + vector.v*vector.v );
} else {
magnitude = vector.magnitude();
}
Sunday, August 15, 2010
Lock Coarsening
StringBuffer buffer = ...
buffer.append( “Hello” );
buffer.append( name );
buffer.append( “\n” );

Sunday, August 15, 2010


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

Sunday, August 15, 2010


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 );
Sunday, August 15, 2010
Other Lock Optimizations
Biased Locking
Adaptive Locking - Thread sleep vs. Spin lock

Sunday, August 15, 2010


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

Sunday, August 15, 2010


Escape Analysis
Point 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


Escape Analysis
Point 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


Run Time Demo

Sunday, August 15, 2010


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

You might also like