Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Overview of Java's Escape Analysis

DZone's Guide to

Overview of Java's Escape Analysis

Understand Escape Analysis and write better and more performant Java code today!

· Java Zone
Free Resource

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

Take Advantage of the JVM's Escape Analysis

In my previous posts:

I elaborated around what Escape Analysis is and how it can improve both our code style and the performance of the code we write. By leveraging Escape Analysis, the JVM can allocate a representation of some of our temporary objects on the stack instead of on the heap. This is a very important performance feature. If all objects were always created on the heap, the garbage collect pressure would be much higher and the overhead of removing unused objects would seriously hamper both performance and performance predictability.

Consider the following method:

  @Override public String toString() {
    final StringBuilder sb = new StringBuilder()
      .append("(")
      .append(x)
      .append(", ")
      .append(y)
      .append(")");
    return sb.toString();
  }

Escape Analysis allows the JVM to create a stack local representation of the StringBuilder Because stack allocation and de-allocation are much faster than heap allocation, our code will run faster. One may argue that the StringBuilder does not formally exist at all because it can never be observed from the outside (by another thread). It is just a temporary representation that can be easily disposed of.

Multi-layer Escape Analysis

Escape Analysis can also work across several call levels so that objects that escape one method but not an overlaying calling method, can be allocated on the stack too.

The Escape Analysis feature makes core Java 8 features such as Stream and Optional run much faster than they otherwise would have. Escape Analysis also speeds up application frameworks that rely on Java 8 Streams, like the open-source database tool http://www.speedment.org/

Write Better Code!

Since we do not have the price of object creation, we can write better and clearer code by using an appropriate abstraction level. Objects can be used as wrappers, to hold call parameters and represent internal method objects with almost no performance penalty at all!

Read the details in the original posts and master Escape Analysis!

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:
java 8 ,jvm ,heap ,performance

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}