4. Java Bytecode
Local Variables
Stack Based
Operand Stack
Local Variable Space
Sunday, August 15, 2010
5. Java Bytecode
Local Variables
Stack Based
Operand Stack
Local Variable Space
3
Sunday, August 15, 2010
6. Java Bytecode
Local Variables
Stack Based
Operand Stack
Local Variable Space
7
3
Sunday, August 15, 2010
7. Java Bytecode
Local Variables
Stack Based
Operand Stack
Local Variable Space
7 +
3
Sunday, August 15, 2010
8. Java Bytecode
Local Variables
Stack Based
Operand Stack
Local Variable Space
3+ 7
Sunday, August 15, 2010
9. Java Bytecode
Local Variables
Stack Based
Operand Stack
Local Variable Space
10
Sunday, August 15, 2010
10. Operation Types
Load and Store
Arithmetic and Logic
Type Conversion
Control Transfer
Object Creation and Manipulation
Operand Stack
Method Invocation
Sunday, August 15, 2010
13. 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
14. Generational Garbage
Collection
Young Generation
Eden
Survivor
Spaces
Old Generation
Tenured
Sunday, August 15, 2010
15. Generational Garbage
Collection
Young Generation
Eden A B
Survivor
Spaces
Old Generation
Tenured
Sunday, August 15, 2010
16. Generational Garbage
Collection
Young Generation
Eden A B C D
Survivor
Spaces
Old Generation
Tenured
Sunday, August 15, 2010
17. Generational Garbage
Collection
Young Generation
Eden A B C D E F G
Survivor
Spaces
Old Generation
Tenured
Sunday, August 15, 2010
18. Generational Garbage
Collection
Young Generation
Eden A B C D E F G
Survivor
Spaces
Old Generation
Tenured
Sunday, August 15, 2010
19. Generational Garbage
Collection
Young Generation
Eden A C D G
Survivor
B E F
Spaces
Old Generation
Tenured
Sunday, August 15, 2010
20. Generational Garbage
Collection
Young Generation
Eden
Survivor
B E F
Spaces
Old Generation
Tenured
Sunday, August 15, 2010
21. 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
22. 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
23. 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
24. Generational Garbage
Collection
Young Generation
Eden
Survivor
B E J
Spaces
Old Generation
Tenured
Sunday, August 15, 2010
25. Generational Garbage
Collection
Young Generation
Eden
Survivor
Spaces
Old Generation
Tenured B E J
Sunday, August 15, 2010
27. 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
29. Optimizations
Just In Time Compilation
Purely Interpreted
Ahead of Time Compilation
Almost No Compile Time Optimization
Most Optimizations are Runtime
Sunday, August 15, 2010
31. 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
32. 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
33. 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
34. 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
35. Common Sub-Expression
Elimination
int x = a + b;
int y = a + b;
Sunday, August 15, 2010
36. 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
37. 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
38. 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
39. Loop Invariant Hoisting
for ( int i = 0; i < nums.length; ++i ) {
...
}
Sunday, August 15, 2010
40. 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
41. Loop Unrolling
int sum = 0;
for ( int i = 0; i < 10; ++i ) {
sum += i;
}
Sunday, August 15, 2010
42. 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