Skip to content

Fix buddy cache evicting chunks with live buffers (#17154) - #17176

Merged
chrisvest merged 1 commit into
netty:4.1from
franz1981:backport-17154-4.1
Jul 30, 2026
Merged

chrisvest merged 1 commit into
netty:4.1from
franz1981:backport-17154-4.1

Conversation

@franz1981

@franz1981 franz1981 commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Motivation:

The shared ConcurrentSkipListChunkCache eviction policy picked the chunk
with the lowest refCnt when the cache exceeded CHUNK_REUSE_QUEUE. Since
chunks enter the cache with refCnt = 1 + N (N live buffers), evicting
them creates zombie chunks: out of the cache, backing buffer still alive
until all N buffers release. This caused 16 GB RSS on the AG benchmark.

Modification:

Only evict chunks where refCnt == 1 (no live buffers — just the base
construction reference). If no idle chunk exists, let the cache grow
past the cap. markToDeallocate on an idle chunk decrements refCnt to 0,
triggering deallocate which frees the backing buffer cleanly.

Result:

Less memory churn under allocation pressure still allowing it to shrink
on subsequence cache requests

(cherry picked from commit franz1981@34bfcc7)

@franz1981
franz1981 requested a review from chrisvest July 30, 2026 09:37
@franz1981
franz1981 force-pushed the backport-17154-4.1 branch from bb18a1a to 31df236 Compare July 30, 2026 09:38
Motivation:

The shared ConcurrentSkipListChunkCache eviction policy picked the chunk
with the lowest refCnt when the cache exceeded CHUNK_REUSE_QUEUE. Since
chunks enter the cache with refCnt = 1 + N (N live buffers), evicting
them creates zombie chunks: out of the cache, backing buffer still alive
until all N buffers release. This caused 16 GB RSS on the AG benchmark.

Modification:

Only evict chunks where refCnt == 1 (no live buffers — just the base
construction reference). If no idle chunk exists, let the cache grow
past the cap. markToDeallocate on an idle chunk decrements refCnt to 0,
triggering deallocate which frees the backing buffer cleanly.

Result:

Less memory churn under allocation pressure still allowing it to shrink
on subsequence cache requests

(cherry picked from commit 34bfcc7)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adjusts the shared Buddy-chunk reuse cache eviction policy in AdaptivePoolingAllocator to avoid evicting/deallocating chunks that still have live buffers, preventing “zombie chunks” and associated RSS growth under allocation pressure.

Changes:

  • Restrict eviction to chunks with refCnt() == 1 (idle: no live buffers).
  • Allow the cache to exceed CHUNK_REUSE_QUEUE when no idle chunks are available to evict.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 621 to +625
Chunk candidate = entry.getValue();
if (candidate != null) {
if (toDeallocate == null) {
toDeallocate = candidate;
key = entry.getKey();
} else {
int candidateRefCnt = candidate.refCnt();
int toDeallocateRefCnt = toDeallocate.refCnt();
if (candidateRefCnt < toDeallocateRefCnt ||
candidateRefCnt == toDeallocateRefCnt &&
candidate.capacity() < toDeallocate.capacity()) {
toDeallocate = candidate;
key = entry.getKey();
}
}
if (candidate != null && candidate.refCnt() == 1) {
toDeallocate = candidate;
key = entry.getKey();
break;
Comment on lines 618 to +622
int key = -1;
Chunk toDeallocate = null;
for (IntEntry<Chunk> entry : chunks) {
Chunk candidate = entry.getValue();
if (candidate != null) {
if (toDeallocate == null) {
toDeallocate = candidate;
key = entry.getKey();
} else {
int candidateRefCnt = candidate.refCnt();
int toDeallocateRefCnt = toDeallocate.refCnt();
if (candidateRefCnt < toDeallocateRefCnt ||
candidateRefCnt == toDeallocateRefCnt &&
candidate.capacity() < toDeallocate.capacity()) {
toDeallocate = candidate;
key = entry.getKey();
}
}
if (candidate != null && candidate.refCnt() == 1) {
@chrisvest chrisvest added this to the 4.1.137.Final milestone Jul 30, 2026
@chrisvest
chrisvest merged commit 8689960 into netty:4.1 Jul 30, 2026
17 of 18 checks passed
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
…y#17176)

Motivation:

The shared ConcurrentSkipListChunkCache eviction policy picked the chunk
with the lowest refCnt when the cache exceeded CHUNK_REUSE_QUEUE. Since
chunks enter the cache with refCnt = 1 + N (N live buffers), evicting
them creates zombie chunks: out of the cache, backing buffer still alive
until all N buffers release. This caused 16 GB RSS on the AG benchmark.

Modification:

Only evict chunks where refCnt == 1 (no live buffers — just the base
construction reference). If no idle chunk exists, let the cache grow
past the cap. markToDeallocate on an idle chunk decrements refCnt to 0,
triggering deallocate which frees the backing buffer cleanly.

Result:

Less memory churn under allocation pressure still allowing it to shrink
on subsequence cache requests

(cherry picked from commit
franz1981@34bfcc7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants