From bbed330468b5b82c9e4defa59012d0fcdb70f1aa Mon Sep 17 00:00:00 2001 From: Alex Petrov Date: Tue, 31 May 2016 23:48:04 +0200 Subject: [PATCH] Fix the possible reference leak in Recycler Motivation: Under very unlikely (however possible) circumstances, Recycler may leak references. This happens _only_ when the object was already recycled at least once (which means it's got written to the stack) and then taken out again, and never returned. The "never returned" part may be the fault of the user (forgotten `finally` clause) or the situation when Recycler drops the possibly youngest item itself. Modifications: Nullify the item taken from the stack. Result: Reference is cleaned up. If the object is lost, it will be a subject for GC. The rest of Stack / Recycler functionality remains unaffected. --- common/src/main/java/io/netty/util/Recycler.java | 1 + 1 file changed, 1 insertion(+) diff --git a/common/src/main/java/io/netty/util/Recycler.java b/common/src/main/java/io/netty/util/Recycler.java index 64983570ee..8b646698e3 100644 --- a/common/src/main/java/io/netty/util/Recycler.java +++ b/common/src/main/java/io/netty/util/Recycler.java @@ -340,6 +340,7 @@ public abstract class Recycler { } size --; DefaultHandle ret = elements[size]; + elements[size] = null; if (ret.lastRecycledId != ret.recycleId) { throw new IllegalStateException("recycled multiple times"); }