String Interning: A Memory-Saving Optimization for Efficient String Handling

Table of Contents

In the realm of Java programming, efficient memory management is crucial for optimizing performance. One of the lesser-known yet powerful techniques employed by Java to conserve memory is string interning. String interning is a mechanism that allows multiple string objects with the same value to share the same memory location, thus reducing memory footprint and improving performance. In this blog post, we’ll explore the concept of string interning, its implementation in Java, its benefits, and when to use it.

What is String Interning?

String interning is the process of storing only one copy of each distinct string value in memory, rather than creating multiple copies. When a string is interned, it is added to a pool of strings called the “string intern pool” or “string constant pool.” Subsequent occurrences of the same string literal or identical string values are then referenced to the same string instance in the pool, rather than creating a new object. This ensures that memory is conserved by eliminating redundancy.

Interning of String object in Java

We can use the intern() method to get the corresponding SCP (String Constant Pool) object reference by using a heap object reference (or by using a heap object reference if we want to get the corresponding SCP object reference, then we should go for the intern method).

Java
String s1 = new String("softAai");
String s2 = s1.intern();
System.out.println(s1 == s2); // false
String s3 = "softAai";
System.out.println(s2 == s3); // true

In the heap area: s1 points to "softAai".

In the SCP area: s2 and s3 point to "softAai".

If the corresponding SCP object is not available, then the intern method itself will create the corresponding SCP object.

Java
String s1 = new String("softAai");
String s2 = s1.concat("Apps");
String s3 = s2.intern();
System.out.println(s2 == s3); // false
String s4 = "softAaiApps";
System.out.println(s3 == s4);

In the heap area:

s1 points to "softAai".

s2 points to "softAaiApps".

In the SCP area:

"softAai", "Apps" are stored individually,

Both s3, and s4 point to "softAaiApps".

Practical Proof of Where Objects Get Created: Heap Area or SCP Area

To illustrate the practical proof of where objects are created, let’s consider the following code:

Java
String s1 = new String("you cannot change me!");
String s2 = new String("you cannot change me!");

System.out.println(s1 == s2); // false

String s3 = "you cannot change me!";

System.out.println(s1 == s3); // false

String s4 = "you cannot change me!";
System.out.println(s3 == s4); // true

String s5 = "you cannot" + "change me!"; // ----> 1
System.out.println(s3 == s5); // true  

String s6 = "you cannot";
String s7 = s6 + "change me!"; // ----> 2

System.out.println(s3 == s7); // false

final String s8 = "you cannot";
String s9 = s8 + "change me!"; // -----> 3

System.out.println(s3 == s9);   // true
System.out.println(s6 == s8);    // true 

In the heap area:

  • s1 points to “you cannot change me!”
  • s2 points to “you cannot change me!”
  • s7 points to “you cannot change me!”

In the SCP area:

  • s3, s4, s5, s9 point to “you cannot change me!”
  • s6, s8 point to “you cannot”
  • “change me!” is stored individually
Note

In the above code, see lines marked as 1, 2, and 3,

Now, let’s analyze the lines marked as —->1, —->2, and —->3:

  1. This operation will be performed at compile time only because both arguments are compile-time constants.
  2. This operation will be performed at runtime only because at least one argument is a normal variable.
  3. This operation will be performed at compile time only because both arguments are compile-time constants.

Importance of String Constant Pool (SCP)

In our program, if a string object is required to be repeated, it is not recommended to create separate objects because it creates performance and memory problems. Instead of creating separate objects for every requirement, we have to create only one object, and we can reuse the same object for every requirement. This approach improves performance and memory utilization. This efficiency is possible because of SCP. Hence, the main advantages of SCP are improved memory utilization and performance.

However, the main problem with SCP is that if several references point to the same object, using one reference to change the content will affect the remaining references. To overcome this problem, Sun implemented string objects as immutable. This means that once we create a string object, we can’t perform any changes to the existing object. If we attempt to make any changes, a new object will be created. Hence, SCP is the primary reason for the immutability of string objects.

Conclusion

String interning is a powerful memory optimization technique in Java that allows for efficient storage and retrieval of string objects. By intelligently managing memory and reducing redundancy, string interning contributes to improved performance and resource utilization in Java applications. Understanding the principles and benefits of string interning empowers Java developers to leverage this technique effectively to optimize their applications for memory and performance efficiency.

Skill Up: Software & AI Updates!

Receive our latest insights and updates directly to your inbox

Related Posts

error: Content is protected !!