Ad

Do I Need To Worry About The String Constant Pool?

- 1 answer

I have a Java application that's very String-heavy - it takes a feed of huge numbers of big, different String objects.

Do I need to worry about the String Constant Pool for memory and performance?

Is there any way to see how big the pool is at any point?

Ad

Answer

As Mario said, the constant pool is only relevant to intern()ed Strings, and to Strings that are constants in java code (these are implicitly interned).

But there is one more caveat that might apply to your case: The substring() method will share the underlying char[] with the original String. So the pattern

  String large = ...                  // read 10k string
  String small = large.substring(...) // extrakt a few chars
  large = null;  // large String object no longer reachable,
                 // but 10k char[] still alive, as long as small lives

might cause unexpected memory usage.

Ad
source: stackoverflow.com
Ad