DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • AI Agents in Java: Architecting Intelligent Health Data Systems

Trending

  • How AI Is Rewriting Full-Stack Java Systems: Practical Patterns with Spring Boot, Kafka and WebSockets
  • Why Your QA Engineer Should Be the Most Stubborn Person on the Team
  • Content Lakes: Harness Unstructured Data for Enterprise AI Readiness
  • Evaluating SOC Effectiveness Using Detection Coverage and Response Metrics
  1. DZone
  2. Coding
  3. Java
  4. Overview of Java's Escape Analysis

Overview of Java's Escape Analysis

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

By 
Per-Åke Minborg user avatar
Per-Åke Minborg
·
Feb. 07, 16 · Analysis
Likes (7)
Comment
Save
Tweet
Share
20.8K Views

Join the DZone community and get the full member experience.

Join For Free

Take Advantage of the JVM's Escape Analysis

In my previous posts:

  • Don't Let Your Java Objects Escape

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!

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • AI Agents in Java: Architecting Intelligent Health Data Systems

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook