Srujan Kumar Gulla
Srujan Kumar Gulla

Reputation: 5851

Java GC tuning for strings

Profiling the application I figured out that there are a lot of strings on heap.

In my situation, strings are created on heap and not interned and they are not literals.

Are there are specific GC tuning techniques to follow when the number of strings in the application are very high.

I stumbled across the GC settings -XX:+UseCompressedStrings or -XX+UseStringCache but not sure this will help. Did any body try these settings?

java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03, mixed mode)

Upvotes: 10

Views: 6998

Answers (2)

Nishit
Nishit

Reputation: 1354

Java 8 comes with a new GC feature -XX:+UseStringDeduplication.

After enabling this, GC would compare strings to find those with same char array value. Let's say str1 & str2 have the same char[], then GC would make str1 point to char[] of str2. This way char[] of str1 can be garbage collected and reclaim the memory. And since Strings are immutable, there is no risk of making multiple strings point to the same char[]

Upvotes: 2

Aleš
Aleš

Reputation: 9028

Related to -XX:+UseCompressedStrings, you should have a look at this question: Support for Compressed Strings being Dropped in HotSpot JVM?

And, related to -XX+UseStringCache, have a look at : JVM -XX:+StringCache argument?

Btw. Java 7 comes with nice features that allow tuning of String cache when using the interned Strings. See -XX:+PrintSTringTableStatistics and -XX:StringTableSize=n. This way you can optimize the String cache size.

Upvotes: 9

Related Questions