Java Reference
In-Depth Information
QUICK SUMMARY
1. Inlining is the most beneficial optimization the compiler can make, particularly
for object-oriented code where attributes are well encapsulated.
2. Tuning the inlining flags is rarely needed, and recommendations to do so often
fail to account for the relationship between normal inlining and frequent inlining.
Make sure to account for both cases when investigating the effects of inlining.
Escape Analysis
The server compiler performs some very aggressive optimizations if escape analysis is en-
abled ( -XX:+DoEscapeAnalysis , which is true by default). For example, consider this class
to work with factorials:
public
public class
class Factorial
Factorial {
private
private BigInteger factorial ;
private
private int
int n ;
public
public Factorial ( int
int n ) {
this
this . n = n ;
}
public
public synchronized
synchronized BigInteger getFactorial () {
iif ( factorial == null
null )
factorial = ...;
return
return factorial ;
}
}
To store the first 100 factorial values in an array, this code would be used:
ArrayList < BigInteger > list = new
new ArrayList < BigInteger >();
for
for ( int
int i = 0 ; i < 100 ; i ++) {
Factorial factorial = new
new Factorial ( i );
list . add ( factorial . getFactorial ());
}
The factorial object is referenced only inside that loop; no other code can ever access that
object. Hence, the JVM is free to perform a number of optimizations on that object:
Search WWH ::




Custom Search