When To Purpose Intern() Method Of String Inward Java?

Advertisement

Masukkan script iklan 970x90px

When To Purpose Intern() Method Of String Inward Java?

Rabu, 07 Maret 2001

The String.intern() method tin last used to bargain amongst the String duplication occupation inwards Java. By carefully using the intern() way y'all tin relieve a lot of heap retentivity consumed past times duplicate String objects. Influenza A virus subtype H5N1 String object is said to last duplicate if it contains the same content every bit to a greater extent than or less other string exactly occupied unlike retentivity place e.g. str1 != str2 exactly str1.equals(str2) is true. Since String object consumes a large amount of String puddle feature provided past times Java. You tin purpose intern() method to intern a String object too shop them into String puddle for farther reuse.


For example, when y'all practice a String literal like "ABC" then it's automatically stored inwards String pool, exactly when y'all practice a novel String object, e.g. new String("abc"), fifty-fifty though it's the same String, a novel object at a unlike retentivity place is created. This is a duplicate String.

By calling the intern() method on this object, y'all tin learn JVM to seat this String inwards the puddle too whenever mortal else creates "abc", this object volition last returned instead of creating a novel object.

This way, y'all tin relieve a lot of retentivity inwards Java, depending upon how many Strings are duplicated inwards your program.  You tin likewise bring together the equals() render true, it volition render the String object from the pool, otherwise it volition add together that object to the puddle of unique String.


2) After calling intern() method on s1 too s2, s1.intern() == s2.intern(), if s1.equals(s2) because both volition last pointing same String constant inwards pool.


3) Prior to Java 6, uncontrolled usage of String.intern() method tin motility java.lang.OutOfMemory: PermGen infinite because String puddle was physically located on PermGen expanse of Java heap, which is quite small-scale inwards many JVM (32M to 96M) too fixed.

From Java vii onward, the intern()  method has expire fifty-fifty to a greater extent than useful because the String puddle is relocated to the master copy heap infinite of JVM.

This volition assist to farther trim String duplication past times using String.intern() method.

In the below diagram y'all tin come across that s3 too s4 are referring to the same String object "java" inwards the String puddle because they are interned String acre s1 too s2 are referring to split upward object because they are non interned or non using String literal.

You tin likewise read String deduplication, which tin trim retentivity footprint caused past times duplicate String without writing a unmarried occupation of code, but, unfortunately, it's alone available for G1 garbage collector too y'all cannot purpose it if y'all are using a ConcurrentMarkSweep garbage collector. You tin likewise banking firm stand upward for out What's New inwards Java viii on Pluralsight to larn to a greater extent than well-nigh G1 GC too other changes introduced inwards Java 8.


Further Learning
The Complete Java Masterclass
Data Structures too Algorithms: Deep Dive Using Java
answer)
  • How the substring() method industrial plant inwards Java? (answer)
  • Why char[] is to a greater extent than secure than String for storing a password inwards Java? (answer)
  • How String inwards switch instance internally implemented inwards Java 7? (answer)
  • The correct way to banking firm stand upward for if String is empty inwards Java? (answer)
  • How to compare 2 String objects inwards Java? (answer)
  • How does String concatenation operate inwards Java? (answer)
  • How to escape String literal inwards Java too Eclipse IDE? (tip)
  • The existent divergence betwixt String, StringBuffer, too StringBuilder inwards Java? (answer)
  • What is String[] args declaration inwards main() method of Java program? (answer)
  • Thanks for reading this article thus far. If y'all similar this Java String article, thus delight portion amongst your friends too colleagues. If y'all accept whatever questions or feedback thus delight drib a note.