Use PermGen space or roll-my-own intern method?
        Posted  
        
            by Adamski
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Adamski
        
        
        
        Published on 2010-05-19T11:29:21Z
        Indexed on 
            2010/05/19
            11:40 UTC
        
        
        Read the original article
        Hit count: 307
        
I am writing a Codec to process messages sent over TCP using a bespoke wire protocol.  During the decode process I create a number of Strings, BigDecimals and dates.  The client-server access patterns mean that it is common for the client to issue a request and then decode thousands of response messages, which results in a large number of duplicate Strings, BigDecimals, etc.
Therefore I have created an InternPool<T> class allowing me to intern each class of object.  Internally, the pool uses a WeakHashMap<T, WeakReferemce<T>>.  For example:
InternPool<BigDecimal> pool = new InternPool<BigDecimal>();
...
// Read BigDecimal from in buffer and then intern.
BigDecimal quantity = pool.intern(readBigDecimal(in));
My question: I am using InternPool for BigDecimal but should I consider also using it for String instead of String's intern() method, which I believe uses PermGen space?  What is the advantage of using PermGen space?
© Stack Overflow or respective owner