Java GC tuning for strings

前端 未结 2 1771
情深已故
情深已故 2021-02-06 06:46

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

2条回答
  •  野性不改
    2021-02-06 07:00

    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[]

提交回复
热议问题