From 0be53f296f8c0342fb9f5c4dd69cd2f3f5bd6ddf Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Fri, 7 Aug 2015 22:37:51 -0700 Subject: [PATCH] MemoryRegionCache$Entry objects are not recycled Motivation: Even though MemoryRegionCache$Entry instances are allocated through a recycler they are not properly recycled, leaving a lot of instances to be GCed along with Recycler$DefaultHandle objects. Fixes #4071 Modification: Recycle Entry when done using it. Result: Less GCed objects. --- .../src/main/java/io/netty/buffer/PoolThreadCache.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/buffer/src/main/java/io/netty/buffer/PoolThreadCache.java b/buffer/src/main/java/io/netty/buffer/PoolThreadCache.java index 3792c3e65a..425f657ca9 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolThreadCache.java +++ b/buffer/src/main/java/io/netty/buffer/PoolThreadCache.java @@ -386,7 +386,14 @@ final class PoolThreadCache { */ @SuppressWarnings("unchecked") public final boolean add(PoolChunk chunk, long handle) { - return queue.offer(newEntry(chunk, handle)); + Entry entry = newEntry(chunk, handle); + boolean queued = queue.offer(entry); + if (!queued) { + // If it was not possible to cache the chunk, immediately recycle the entry + entry.recycle(); + } + + return queued; } /** @@ -398,6 +405,7 @@ final class PoolThreadCache { return false; } initBuf(entry.chunk, entry.handle, buf, reqCapacity); + entry.recycle(); // allocations is not thread-safe which is fine as this is only called from the same thread all time. ++ allocations;