Search code examples
javamemory-managementgarbage-collectionpermgenstring-pool

If string pool is a part of Perm Gen Space, does it get garbage collected? And if it does, does it get collected during minor GC or full GC?


I have read that string pools are actually part of perm generation. And string pools do not generally get garbage collected, as a reference still exists even after GC runs and references are no longer used. So does it really get garbage collected? And as far as perm gen is concerned, it requires a full GC for the garbage collection. So collection happens or not? Kindly help with the confusion


Solution

  • In short, JVM String pool is collected during major GC.

    String object in pool are subject for normal Java garbage collection rules.

    String pool is similar to WeakHashMap in its semantic. Once String object doesn't have any strong reference it would be collected and reference would be removed from pool.

    Prior to Java 7, String literal in pool may be collected only if perm gen is included in collection (not every Full/Old GC includes perm gen in scope).

    In Java 7 and Java 8 pool itself and pooled string literals are part of normal heap.

    You can find more details in this article.