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 String
s, BigDecimal
s 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 String
s, BigDecimal
s, etc.
Therefore I have created an InternPool<T>
class allowing me to intern each class of object. Internally, the pool uses a WeakHashMap<T, WeakReference<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?
WeakReference
appropriate for this, or should you rather be using aSoftReference
? The GC behaves differently for both and this sounds like you are trying to create a kind of cache; weak references are not good use for that purpose. See my answer here for some reasons why: #2861910 – Shankbyte[]
s from the map once a BigDecimal was enqueued. (Probably need a BiMap). This can eliminate the construction of redundant BigDecimal objects saving memory/gc runtime and execution time (only have to construct once). – Heliotherapybyte[]
and only convert to BigDecimal once you actually need to use it. This operation too can be cached. This gives the simplicity ofbyte[] b = pool.intern(getBytes());
with the benefits of lazy construction. In both cases you'll have to read bytes (or whatever you ctor your BigDecimals with) but in this case you will only ctor 1 of each unique BigDecimal. – Heliotherapy