Garbage collection behaviour for String.intern()

前端 未结 4 1602
北恋
北恋 2020-11-27 07:31

If I use String.intern() to improve performance as I can use \"==\" to compare interned string, will I run into garbage collection issues? How does the garbage collection me

4条回答
  •  攒了一身酷
    2020-11-27 08:01

    This article provides the full answer.

    In java 6 the string pool resides in the PermGen, since java 7 the string pool resides in the heap memory.

    Manually interned strings will be garbage-collected.
    String literals will be only garbage collected if the class that defines them is unloaded.

    The string pool is a HashMap with fixed size which was small in java 6 and early versions of java 7, but increased to 60013 since java 7u40.
    It can be changed with -XX:StringTableSize= and viewed with -XX:+PrintFlagsFinal java options.

提交回复
热议问题