Optimize memory usage of a collection of Strings in Java

后端 未结 7 1746
有刺的猬
有刺的猬 2021-01-04 21:21

I have a large number of name - value pairs (approx 100k) that I need to store in some sort of cache (say a hash map) where the value is a string with an average of about 30

7条回答
  •  爱一瞬间的悲伤
    2021-01-04 22:12

    Agreed with others on not using String.intern(): once you've put a string there, it will never go away. Look to early revisions of Xerces for why this is a bad idea.

    A better solution is to use a WeakHashMap, wrapping the value in a WeakReference:

    private Map> _map 
        = new WeakHashMap>();
    
    public synchronized String intern(String str)
    {
        WeakReference ref = _map.get(str);
        String s2 = (ref != null) ? ref.get() : null;
        if (s2 != null)
            return s2;
        str = new String(str);
        _map.put(str, new WeakReference(str));
        return str;
    }
    

    This code is from an article that I wrote on the Java reference objects. You'll find the explanation there.

    EDIT: need to create a new string here (and I'll update the article) because the original might be a substring of a far larger character array. I thought that was fixed around JDK 1.3, but apparently not (at least not in 1.5).

提交回复
热议问题