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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Writing DTOs With Java8, Lombok, and Java14+
  • Formatting Strings in Java: String.format() Method
  • Hibernate Validator vs Regex vs Manual Validation: Which One Is Faster?
  • Functional Approach To String Manipulation in Java

Trending

  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  • Performance Optimization Techniques for Snowflake on AWS
  • Build an MCP Server Using Go to Connect AI Agents With Databases
  • Accelerating AI Inference With TensorRT
  1. DZone
  2. Coding
  3. Java
  4. Java String Intern: Performance Impact

Java String Intern: Performance Impact

java.lang.String#intern() is an interesting function in Java. In this post, let’s discuss the performance impact of using this function in your application.

By 
Ram Lakshmanan user avatar
Ram Lakshmanan
DZone Core CORE ·
Sep. 19, 22 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
7.1K Views

Join the DZone community and get the full member experience.

Join For Free

java.lang.String#intern() is an interesting function in Java. When used in the right place, it has the potential to reduce the overall memory consumption of your application by eliminating duplicate strings in your application. To learn how the intern() function works, you may refer to this blog. In this post, let’s discuss the performance impact of using java.lang.String#intern() function in your application.

Intern() Function Demo

To study the performance behavior of the intern() method, we created these two simple programs:

Java
 
public class InternDemo {    
   
   private static List<String> datas = new ArrayList<>(10_000_000);      
   
   public static void main(String args[]) throws Exception {         

      BufferedReader reader = new BufferedReader(new FileReader("C:\\workspace\\random-data.txt"));      
      String data = reader.readLine();      
      while (data != null) {         
         data = reader.readLine().intern();         
         datas.add(data);      
      }      
      reader.close();     
   } 
}
Java
 
public class NoInternDemo {    

   private static List<String> datas = new ArrayList<>(10_000_000);      

   public static void main(String args[]) throws Exception {         
      
      BufferedReader reader = new BufferedReader(new FileReader("C:\\workspace\\random-data.txt"));      
      String data = reader.readLine();      
      while (data != null) {         
        data = reader.readLine();         
        datas.add(data);      
     }      
     reader.close();      
  } 
}


I request you to review the above source code, before reading further. It’s a simple program. If you notice, the ‘InternDemo‘ program reads each line at a time from the ‘random-data.txt‘ & then invokes the intern() operation on the read data. String returned by the intern() function is then added to the ‘datas’ ArrayList. ‘NoInternDemo’ program also does the exact same thing, the only difference is ‘NoInternDemo’ doesn’t invoke the ‘intern()‘ operation and ‘InternDemo‘ invokes the ‘intern()‘ operation. 

You also need to understand the contents of the ‘random-data.txt‘. This file contains 10 million UUID (Universally Unique Identifiers) strings. Even though there are 10 million UUID strings in this file, there is a significant amount of duplication among them. Basically, there are only 10 unique UUIDs, which are inserted 10 million times into this file. Data is intentionally structured in such a manner that there is a heavy number of duplicate strings in this file. You can download the ‘random-data.txt’ file that we used for this experiment from this location.

Memory Impact

We executed both programs. Before the program's exit, we captured the heap dump from them. A heap dump is basically a snapshot of memory, which contains information about all the objects that are residing in the memory. We studied the heap dump through the HeapHero – a heap dump analysis tool. Here are the live reports generated by this tool:

  1.  InternDemo Heap analysis report 
  2. NoInternDemo Heap analysis report 

 The below table summarizes the difference between both the programs:

                                                                                 InternDemo                                  NoInternDemo                                
Total Size  38.37MB 1.08GB
Object Count 4,184 20,004,164
Class Count 456 456

You can notice that ‘InternDemo’ has only 4k+ objects consuming 38.37MB only, whereas ‘NoInternDemo’ has 20 million+ objects consuming 1.08GB of memory. Basically ‘NoInternDemo’ consumes 28 times more memory than ‘InternDemo’. This demo clearly illustrates that memory optimization is achieved through the intern() function.

Duplicate Strings Impact

The HeapHero tool in its report indicates how much memory is wasted due to inefficient programming practices. We noticed that ‘InternDemo’ is not wasting any memory, whereas ‘NoInternDemo’ is wasting 1.04GB (i.e. 97%) of memory due to inefficient programming practices. In this 97% memory wastage, 96.5% of wastage is surfacing due to duplicate strings. 

Fig: InternDemo memory wastage reported by HeapHero

InternDemo memory wastage reported by HeapHero

Fig: NoInternDemo memory wastage reported by HeapHeroNoInternDemo memory wastage reported by HeapHero

The tool also points out that the following are the duplicate strings that are present in the memory. Basically, these 10 strings are the 10 unique UUIDs that were present in the random-data.txt file. Because of duplication, each string was wasting 106MB of memory. If the intern() operation was used, then this kind of memory wastage could have been avoided.

Duplicate Strings

Duplicate Strings that are present in NoInternDemo reported by HeapHero

Response Time Impact

We executed both the ‘InternDemo’ and ‘NoInternDemo’ programs a few times. The Below chart shows the average response time of these two programs:

     InternDemo                                                                                NoInternDemo                                                       
     2042 ms        1164 ms

Respose time

Basically ‘InternDemo’ was 75% slower than ‘NoInternDemo’. It’s just because ‘InternDemo’ must invoke string.equals() method on all the objects in the String intern pool for the 10 million records. Hence it consumes more CPU and time. Thus response time of the ‘InternDemo’ is slower than ‘NoInternDemo’. No wonder people say: "There is no free lunch." ‘InternDemo’ was highly performant from the memory perspective. ‘NoInternDemo’ was highly performant from the CPU/response time perspective. 

NOTE: The performance impact of the intern() function is heavily dependent on the data that your application processes. In the above example, there was a heavy number of duplicate strings, thus you saw a great reduction in memory utilization and a spike in response time. This may not be the same behavior in all applications. You have to perform proper testing before using the intern() function in your application.

Video

Java (programming language) Strings

Opinions expressed by DZone contributors are their own.

Related

  • Writing DTOs With Java8, Lombok, and Java14+
  • Formatting Strings in Java: String.format() Method
  • Hibernate Validator vs Regex vs Manual Validation: Which One Is Faster?
  • Functional Approach To String Manipulation in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!