🎉 Special Offer !    Code: GET300OFF    Flat ₹300 OFF on every Java Course
Grab Deal 🚀

Memory Management via String Constant Pool  


Introduction
  • Strings are widely used in Java programs, so efficient memory management is important.
  • Java provides a special memory area called String Constant Pool (SCP) to store string literals and reuse them, reducing duplicate objects.
  • So, let's understand what String Constant Pool is, how it works, and how String achieves memory management in Java.

String Constant Pool (SCP):
  • String Constant Pool (also known as String Literal Pool) is a special memory region inside the Heap area where Java stores string literals for memory efficiency and reusability.
  • In Java, there are two common ways to create String objects, and their memory allocation differs:
    1. Using String Literals:
      • Example: String name = "Deepak";
      • The literal "Deepak" is stored in the String Constant Pool.
      • If the same literal already exists in the SCP, Java will not create a new object. The reference variable name will point to the already existing object.
    2. Using the new Keyword:
      • Example: String name = new String("Amit");
      • This creates two objects:
        • One in the Heap area due to the new keyword.
        • One in the String Constant Pool (SCP) if the literal "Amit" does not already exist.
      • The reference variable name points to the Heap object, not the SCP object.
  • Below is a diagram illustrating both cases: String Constant Pool

Points about String Constant Pool (SCP):
  1. How it gets its name?
    • The term “Constant Pool” is used because only string constants (literals) are stored here.
    • Whenever you write something like String s = "Java";, the literal "Java" is placed in this pool.
  2. Memory Location:
    • SCP is a part of the heap area, but it is managed separately by JVM.
    • This ensures that duplicate literals are not created.
  3. Reusability:
    • If another variable tries to store the same literal, the JVM reuses the existing object from SCP instead of creating a new one.
    • Example:
      String s1 = "Hello";
      String s2 = "Hello";  // s2 points to the same object as s1
      System.out.println(s1 == s2); // true
  4. Objects Created Using new Keyword:
    • If we use new, the object is created in the heap (outside SCP) and not reused.
    • Example:
      String s1 = new String("World");
      String s2 = new String("World");
      System.out.println(s1 == s2); // false (different objects in heap)
  5. Garbage Collection in SCP:
    • Normally, objects in the heap are eligible for Garbage Collection (GC) if no reference is pointing to them.
    • But objects in the String Constant Pool (SCP) are treated specially:
      • Even if no reference is pointing to the string literal, it is not garbage collected immediately.
      • It usually stays in memory until the JVM itself shuts down.
    • This is because literals are reused by the JVM to save memory.
  6. Performance Benefit:
    • Since string literals are reused from SCP, memory usage is reduced and performance is improved compared to creating new objects every time.

Use of intern() method in SCP:
  • The intern() method in Java is used for memory optimization.
  • It helps ensure that only one copy of each distinct string exists in the JVM's string pool (SCP), which saves memory when the same string is used multiple times in your program.
  • How it works:
    • Lets first see the example:-
      String s1 = new String("Hello");   // Created in Heap
      String s2 = s1.intern();           // Returns reference from String Pool (SCP)
      String s3 = "Hello";               // Literal in String Pool (SCP)
      
      System.out.println(s1 == s2);  // false  (s1 is from Heap, s2 from SCP)
      System.out.println(s2 == s3);  // true   (both s2 and s3 are from SCP)
    • If the string already exists in the string pool (SCP):
        intern() will return the same reference to that string, instead of creating a new one.
    • If the string doesn’t exist in the SCP:
      • The string is added to the string pool, and its reference is returned.
    • This helps with memory optimization because it prevents creating multiple copies of the same string in memory.
    • By using intern(), you can compare strings with the == operator. This works because both strings that are interned point to the same object in the string pool.
    • The intern() method works because strings in Java are immutable. This means their values can't be changed once created, so they can safely be shared between multiple parts of a program. However, mutable objects can't be shared like this, as their content can change.