我正在编写一个编解码器来处理使用定制的有线协议通过 TCP 发送的消息。在解码过程中,我创建了许多String
s、BigDecimal
s 和日期。客户端-服务器访问模式意味着客户端发出请求然后解码数千条响应消息是很常见的,这会导致大量重复 String
的s,BigDecimal
s等。
因此,我创建了一个InternPool<T>
类,允许我实习每个类的对象。在内部,池使用WeakHashMap<T, WeakReference<T>>
. 例如:
InternPool<BigDecimal> pool = new InternPool<BigDecimal>();
...
// Read BigDecimal from in buffer and then intern.
BigDecimal quantity = pool.intern(readBigDecimal(in));
我的问题:我正在使用InternPool
forBigDecimal
但我是否应该考虑也使用它String
而不是 String
'intern()
方法,我认为该方法使用 PermGen 空间?使用 PermGen 空间有什么好处?