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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

Related

  • Java Memory Management
  • Using Heap Dumps to Find Memory Leaks
  • Understanding Root Causes of Out of Memory (OOM) Issues in Java Containers
  • Java Z Garbage Collector (ZGC): Revolutionizing Memory Management

Trending

  • Cosmos DB Disaster Recovery: Multi-Region Write Pitfalls and How to Evade Them
  • Building Resilient Identity Systems: Lessons from Securing Billions of Authentication Requests
  • The Role of AI in Identity and Access Management for Organizations
  • Monolith: The Good, The Bad and The Ugly
  1. DZone
  2. Coding
  3. JavaScript
  4. G1 GC: Reducing Memory Consumption by 20%

G1 GC: Reducing Memory Consumption by 20%

This quick runthrough will help you turn on string deduplication for G1 garbage collection, as well as analyzes its impact on your app's memory footprint.

By 
Ram Lakshmanan user avatar
Ram Lakshmanan
DZone Core CORE ·
May. 07, 18 · Tutorial
Likes (22)
Comment
Save
Tweet
Share
62.8K Views

Join the DZone community and get the full member experience.

Join For Free

Modern Java applications do a lot of string manipulations due to web service API calls (i.e. JSON, REST, SOAP, …), external data source calls (SQL, data returned back from DB, …), text parsing, text building, etc. Thus, string objects can easily occupy at least 30% of memory. Apparently, the majority of those String objects are duplicates. Because of string duplication, a considerable amount of memory is wasted. Thus, to optimize the memory wasted by duplicate string objects, JEP 192 has become a welcome enhancement to Java.

What Does JEP 192 Do?

When the G1 GC algorithm runs, it removes garbage objects from memory. It also removes duplicate string objects from memory — string deduplication. This feature can be activated by passing the following JVM arguments:

-XX:+UseG1GC -XX:+UseStringDeduplication


Note 1: In order to use this feature, you need to run on Java 8 update 20 or later versions.

Note 2: In order to use ‘-XX:+UseStringDeduplication’, you need to be using the G1 GC algorithm.

Let’s Study With an Example

Let’s validate this feature with this simple program. This example has been chosen basically to study how the JVM handles duplicate strings.

public class StringDeduplicationExample {
    public static List<String>myStrings = new ArrayList
    public static void main(String[] args) throws Exception {
        for (int counter = 0; counter < 200; ++counter) {
            for (int secondCounter = 0; secondCounter < 1000; ++secondCounter) {
                // Add it 1000 times.
                myStrings.add(("Hello World-" + counter));
            }
                     System.out.println("Hello World-" + counter + " has been added 1000 times");
        }
    }
}


This program basically creates:

  • 1000 instances of “Hello World-0” strings
  • 1000 instances of “Hello World-1” strings
  • 1000 instances of “Hello World-2” strings
  • ...
  • ...
  • ...
  • 1000 instances of “Hello World-199” strings

We ran this program a couple of times with two different JVM arguments.

Run #1

The first time we ran the program by passing ‘-XX:+UseStringDeduplication’ JVM argument. i.e.:

-Xmx20M -XX:+UseG1GC -XX:+UseStringDeduplication


Run #2

The second, time we ran the same program without passing the ‘-XX:+UseStringDeduplication’ argument:

-Xmx20M -XX:+UseG1GC


During both the runs, we captured heap dumps and analyzed them through the heap dump analysis tool HeapHero.io. HeapHero.io detects the amount of memory wasted due to various inefficient programming practices, including the amount of memory wasted due to duplicate strings.

Here are the reports generated by HeapHero.io:

  1. Heap Dump analysis report for Run #1
  2. Heap Dump analysis report for Run #2

Here are few interesting observations from the report


Run #1 (argument passed)

Run #2 (argument not passed)

Overall Heap Size

7.94mb

15.89mb

Total # of Strings

206k

206k

Size of memory wasted due to Duplicate Strings

5.6mb

13.81mb


Even though the same code was executed, in Run #1 (where ‘-XX:+UseStringDeduplication’ was passed), you can see the overall heap size was 7.94mb, whereas in Run #2 (‘-XX:+UseStringDeduplication’ is not passed), there is a considerable increase in the overall heap size — 15.89mb.

Even though there is an equivalent number of string objects in both the runs (206k), the amount of memory wasted due to duplicate strings in Run #1 is 5.6mb, whereas in Run #2, it was 13.81mb.

This dramatic reduction in memory consumption was made possible because of the ‘-XX:+UseStringDeduplication’ argument, which evicted a significant number of duplicate strings from the application.

Thus we encourage you to take advantage of ‘-XX:+UseG1GC -XX:+UseStringDeduplication’ and reduce memory wastage caused by duplicate strings. This change has the potential to reduce the overall memory footprint of your application.

garbage collection Memory (storage engine) Strings

Opinions expressed by DZone contributors are their own.

Related

  • Java Memory Management
  • Using Heap Dumps to Find Memory Leaks
  • Understanding Root Causes of Out of Memory (OOM) Issues in Java Containers
  • Java Z Garbage Collector (ZGC): Revolutionizing Memory Management

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!