Module: | Classes, Objects, OOPs & Inheritance
Q41: Consider the following statements regarding String Interning and Garbage Collection:
1. The 'intern()' method allows a developer to manually force a dynamically created Heap String into the String Constant Pool, returning the highly optimized, canonical pooled reference.
2. Prior to Java 7, the String Constant Pool resided in the rigid PermGen space, frequently causing severe 'OutOfMemoryError' application crashes because the PermGen was not aggressively targeted by the Garbage Collector.
3. Modern Java implementations completely disable Garbage Collection on the String Constant Pool to structurally ensure that application startup constants are permanently cached in memory forever.
Which of the above statements is/are correct?
2. Prior to Java 7, the String Constant Pool resided in the rigid PermGen space, frequently causing severe 'OutOfMemoryError' application crashes because the PermGen was not aggressively targeted by the Garbage Collector.
3. Modern Java implementations completely disable Garbage Collection on the String Constant Pool to structurally ensure that application startup constants are permanently cached in memory forever.
Which of the above statements is/are correct?
✅ Correct Answer: A
🎯 Quick Answer:
The correct combination is 1 and 2. Statement 3 is incorrect because modern Java implementations aggressively subject the String Constant Pool to Garbage Collection. Unreferenced pooled strings are dynamically deleted to prevent major memory leaks.The '.intern()' method serves as a bridge to push dynamically generated strings into this highly efficient cache.
Structural Breakdown: Calling 'myString.intern()' executes a native C++ function within the JVM.
It scans the pool for an exact match.
If found, it returns the pooled reference.
If not, it registers the string into the pool and returns its new reference.
Historical/Related Context: The relocation of the String Pool in Java 7 was a massive architectural milestone.
In Java 6, because the PermGen space had a severely restricted fixed size (often defaulting to just 64MB) and poor GC sweeping, developers who heavily utilized '.intern()' quickly overflowed the PermGen, fatally crashing the application.
Causal Reasoning: By relocating the pool to the vast, main Heap memory area (Statement 2 context), the String Pool gained the ability to dynamically expand using the host machine's available RAM and fully participate in standard, highly optimized Garbage Collection cycles, completely curing the notorious legacy OutOfMemory anomalies.