Garbage collection behaviour for String.intern()

String.intern() manages an internal, native-implemented pool, which has some special GC-related features. This is old code, but if it were implemented anew, it would use a java.util.WeakHashMap. Weak references are a way to keep a pointer to an object without preventing it from being collected. Just the right thing for a unifying pool such as interned strings.

That interned strings are garbage collected can be demonstrated with the following Java code:

public class InternedStringsAreCollected {

    public static void main(String[] args)
    {
        for (int i = 0; i < 30; i ++) {
            foo();  
            System.gc();
        }   
    }

    private static void foo()
    {
        char[] tc = new char[10];
        for (int i = 0; i < tc.length; i ++)
            tc[i] = (char)(i * 136757);
        String s = new String(tc).intern();
        System.out.println(System.identityHashCode(s));
    }
}

This code creates 30 times the same string, interning it each time. Also, it uses System.identityHashCode() to show what hash code Object.hashCode() would have returned on that interned string. When run, this code prints out distinct integer values, which means that you do not get the same instance each time.

Anyway, usage of String.intern() is somewhat discouraged. It is a shared static pool, which means that it easily turns into a bottleneck on multi-core systems. Use String.equals() to compare strings, and you will live longer and happier.

Leave a Comment