This article looks at a Java Virtual Machine (JVM) feature called Escape
Analysis in some detail and how the JVM can use it to improve an
application's performance. As you'll see, understanding what the JVM can do
with escape analysis can help explain some otherwise non-intuitive
performance results.
A Performance Puzzle
Let's assume you have a utility class that generates values by keeping some
internal state of the last value generated and using it to arrive at the next
value. Since the class maintains some internal state, and since this is a
utility class that you might use in multithreaded environments, you decide to
synchronize the getNext() method. You might code it as:
public class Generator {
private int lastgen; // saved state
Generator(int value) {
// initialize lastgen using supplied value
}
public synchronized int getNext() {
int nextgen = somefunction(la... (more)