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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Trending

  • Member Spotlight: Shai Almong
  • Beyond Manual Mapping: AI-Orchestrated Governance for Hybrid Cloud
  • How to Build a Scalable SaaS Product Architecture
  • RAG Architecture Patterns: Proxy, Caching, and Routing

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Feb. 07, 16 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
21.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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