Thursday, April 25, 2024
HomeJavaWhat's String deduplication in Java? Save Reminiscence from Duplicate String in...

What’s String deduplication in Java? Save Reminiscence from Duplicate String in Java 8? [Answer]


You may not bear in mind that Java 8 replace 20 has launched a brand new characteristic referred to as “String deduplication” which can be utilized to avoid wasting reminiscence from duplicate String objects in Java software, which might enhance the efficiency of your Java software and forestall java.lang.OutOfMemoryError in case your software makes heavy use of String. You probably have profiled a Java software to test which object is taking the majority of reminiscence, you’ll typically discover char[] object on the prime of the record, which is nothing however an inside character array utilized by String object. Among the instruments and profilers may present this as java.lang.String[] as properly like Java Flight Recorder, however they’re basically pointing to the identical downside i.e. a serious portion of reminiscence is occupied with String objects.

Since from Java 7 onward, String has stopped sharing character array with sub-strings, the reminiscence occupied by String object has gone increased, which had made the issue even worse.
If you happen to keep in mind, earlier each substring and String share the identical character objects (see how Substring works in Java), which was truly a bug that had the potential to trigger a critical reminiscence leak. The bug was fastened in JDK 7, however it created this new downside.

The String deduplication is making an attempt to bridge that hole. It reduces the reminiscence footprint of String objects on the Java Heap area by profiting from the truth that many String objects are similar. As an alternative of every String object pointing to its personal character array, similar String objects can level to the identical character array.

Btw, this isn’t precisely the identical because it was earlier than Java 7 replace 6, the place substring additionally factors to the identical character array, however can tremendously scale back reminiscence occupied by duplicate String in JVM. Anyway, On this article, you will notice how one can allow this characteristic in Java 8 to scale back reminiscence consumed by duplicate String objects.

Btw, in case you are not aware of new options on Java 8 then I counsel you first undergo a complete and up-to-date Java course like The Full Java MasterClass on Udemy. It is also very inexpensive and you should buy in simply $10 on Udemy gross sales which occur every so often.

allow String deduplication in Java 8

String deduplication is not enabled by default in Java 8 JVM. You may allow the String deduplication characteristic by utilizing -XX:+UseStringDeduplication choice. Sadly, String deduplication is solely out there for the G1 rubbish collector, so in case you are not utilizing G1 GC you then can’t use the String deduplication characteristic.

It means simply offering -XX:+UseStringDeduplication is not going to work, you additionally have to activate the G1 rubbish collector utilizing -XX:+UseG1GC choice.

String deduplication additionally does not contemplate comparatively younger String for processing. The minimal age of processed String is managed by -XX:StringDeduplicationAgeThreshold=3 choice. The default worth of this parameter is 3.


Now, you is likely to be considering that how does this methodology examine with the normal strategy to scale back reminiscence as a result of duplicate String e.g. by utilizing intern() methodology of java.lang.String class? Properly, this method has a bonus since you need not write a single line of code. Simply allow this characteristic utilizing JVM parameters and you’re accomplished.

You probably have ever optimized your code by utilizing String.intern() methodology then you recognize that it is not straightforward. It not solely compromise readability by including further traces of code with out including any performance but in addition improve the scale of the code.  

Btw, in case you are serious about studying extra about G1 Rubbish collectors,  I counsel testing these Superior Java Efficiency programs to be taught JVM Tuning, Rubbish Assortment, and Perperofmrance tuning.  which covers some good details about G1 Rubbish collectors.

Necessary factors about String Deduplication Options

Listed here are among the vital factors concerning the String deduplication characteristic of Java 8:

1. This feature is simply out there from Java 8 Replace 20 JDK launch.

2. This characteristic will solely work together with the G1 rubbish collector, it is not going to work with different rubbish collectors like Concurrent Mark Sweep Rubbish collector.

3. It’s essential present each -XX:+UseG1GC and -XX:+StringDeduplication JVM choices to allow this characteristic, the primary one will allow the G1 rubbish collector and the second will allow the String deduplication characteristic inside G1 GC.

4. You may optionally use -XX:+PrintStringDeduplicationStatistics JVM choice to investigate what is going on via the command line.

5. Not each String is eligible for deduplication, particularly younger String objects usually are not seen, however you’ll be able to management this by utilizing  -XX:StringDeduplicationAgeThreshold=3 choice to alter when Strings turn out to be eligible for deduplication.

6. It’s noticed basically this characteristic might lower heap utilization by about 10%, which is superb, contemplating you do not have to do any coding or refactoring.

7. String deduplication runs as a background activity with out stopping your software.

If you wish to be taught extra about Java efficiency, JVM choices, and profiling Java functions, I counsel studying Java Efficiency Companion by Charlie Hunt. It’s an up-to-date e-book among the finest books to be taught and perceive instruments and methods of Java efficiency tuning It additionally contained some actually good details about G1 Rubbish Collector, which is extra related associated to Sring deduplication.
Use String Deduplication to Save Memory from Duplicate String in Java 8

That is all about easy methods to use allow String deduplication in Java 8 to scale back reminiscence consumed by duplicate String objects. This is without doubt one of the helpful options to learn about it however sadly, it is just out there for G1 Rubbish Collector. You additionally want Java 8 Replace 20 to make use of to allow this feature. 

Hopefully, in Java 9, when the G1 Rubbish collector will turn out to be the default collector, it might probably use this characteristic to additional enhance efficiency. If we’re fortunate, we might also see this characteristic prolonged for different main rubbish collectors just like the Concurrent Mark Sweep Rubbish collector.

Different Java GC and JVM Articles it’s possible you’ll like

Thanks for studying this text up to now. If you happen to discovered my clarification of the String deduplication characteristic of Java JVM then please share it with your pals and colleagues. You probably have any questions, be at liberty to ask. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments