GeezerVison is showing github.com — Adaptive allocator may retain chunks beyond reuse cap and exhaust direct memory under burst load · Issue #18434 · netty/netty
Adaptive allocator may retain chunks beyond reuse cap and exhaust direct memory under burst load #18434DescriptionTitle
Netty 4.2.18.Final Fails at Lower Concurrency Due to Direct Memory Exhaustion
Expected Behavior
With -Dio.netty.allocator.type=adaptive and -XX:MaxDirectMemorySize=256m, Netty 4.2.x should support approximately the same concurrency as 4.1.x, or document any intentional differences.
Actual Behavior
Netty 4.2.18.Final reportedly fails at roughly half the concurrency of 4.1.138.Final with java.nio.Bits.reserveMemory errors, including during channel establishment.
JFR showed 227 allocated 1 MiB chunks and 99 released, leaving 128 MiB reserved. Much of this memory reportedly no longer held live buffers.
Steps to Reproduce
- Use DataStax Java Driver 4.19.1 with Cassandra over TLS.
- Configure:
-XX:MaxDirectMemorySize=256m
-Dio.netty.allocator.type=adaptive
- Use the default I/O thread count (32 on the test host).
- Increase concurrent inserts with 100 KB payloads.
- Compare the maximum sustainable concurrency across Netty versions.
Environment
- Netty: 4.2.18.Final vs. 4.1.138.Final
- JDK: 21
- OS: Linux(ClouddCFoundry container, 16 CPUs visible to the JVM)
Suspected Related Change
[34bfcc7d](https://github.com/netty/netty/commit/34bfcc7d04363c1290bf81fed203cd93ca1c7764) — Fix buddy cache evicting chunks with live buffers.
Additional Context
In Netty 4.1.138.Final, ConcurrentSkipListChunkCache.offerChunk evicted the cached chunk with the fewest live references when the cache exceeded CHUNK_REUSE_QUEUE.
Question
Given the eviction-policy change in 4.2.18.Final, should the same policy be backported to 4.1 so both branches behave consistently, or is the difference intentional? If it is intentional, what memory-use behavior should users expect from the adaptive allocator under burst load?
I’d be happy to contribute either a backport of the 4.2 change to 4.1 or a change that reverts the commit, depending on which direction maintainers recommend. Activityfranz1981 commented on Nov 16, 2026 Thanks for the detailed report. A correction on the premise first: #17154 was backported to 4.1 in #17176, but a later backport PR (#17206) accidentally put the old eviction loop back. So the 4.1/4.2 difference isn't intentional. 4.1 should get the fix again rather than 4.2 reverting it.
The old policy evicted chunks that still had live buffers. Those chunks couldn't be reused but kept their memory until every buffer was released, which caused severe RSS blow-up and churn in our API-gateway allocator benchmark (16.5 GB vs 2.4 GB RSS).
Your data does show a gap in the current policy: idle chunks above the cap are only trimmed on the next offerChunk, so after a burst they can stay allocated. Your 100 KB buffers go to the shared buddy cache, whose cap is 2 * availableProcessors chunks, so about 32 MiB with 16 CPUs and 1 MiB chunks; ~128 MiB retained fits the cache growing past it. To confirm that's the cause, could you:
- Run 4.2.18 with only 34bfcc7 reverted (same driver and settings) and compare max concurrency.
- At the point of failure, share the JDK direct pool usage (
BufferPoolMXBean named "direct": getMemoryUsed()/getCount()) next to AdaptiveByteBufAllocator.metric().usedDirectMemory(). With JDK 21 defaults, Netty's chunks are allocated through ByteBuffer.allocateDirect (hence the Bits.reserveMemory failure) and share the 256 MiB budget with any other direct buffers in the process, so this tells us how much of it is the allocator.
- Share the JFR
AllocateChunkEvent/FreeChunkEvent data including capacity, pooled and threadLocal, so we can separate the 1 MiB buddy chunks from the size-class ones.
If (1) recovers the 4.1 numbers, the fix is to also trim idle chunks outside offerChunk, not to bring back eviction of live chunks.
Update: I reproduced it with a synthetic test on 4.2.18 (32 threads, 100 KB buffers, 16 CPUs, -XX:MaxDirectMemorySize=256m). After a burst, the buddy cache keeps ~180 idle 1 MiB chunks (cap 32) indefinitely, because nothing calls offerChunk once the burst is over. A following load of small buffers then fails with Cannot reserve 131072 bytes, while with 34bfcc7 reverted usage drops back to ~55 MiB and the same load passes. So this is a regression from #17154: idle chunks need to be trimmed even when no further offers arrive. Reacted by tarakeshpolakolu tarakeshpolakolu commented on Nov 16, 2026 Thanks for the detailed investigation and for reproducing the regression. Given these findings, what do you think is the best way forward to fix this? Should we introduce a mechanism to trim idle chunks independently of offerChunk, and if so, what would be the most appropriate approach without reintroducing the original live-chunk eviction issue? franz1981 commented on Nov 16, 2026 A correction to my update above: what I reproduced is narrower than your report. If the 100 KB load stops on all threads at the same moment, the chunks that just went idle stay in the cache above its cap, because nothing calls offerChunk afterwards, and that memory can then starve other allocations.
I could not reproduce the lower max concurrency. In my steady-load tests (32 threads, 100 KB buffers, 16 CPUs, -XX:MaxDirectMemorySize=256m), 4.2.18 sustains more than the old policy, not less: ~224 MiB in flight vs ~128 MiB, with ~2x faster allocations and far fewer chunk mallocs. That doesn't match your numbers, so before deciding on a fix we need more data:
- a reproducer, or the exact load pattern of your test (how concurrency ramps, and whether load drops between steps);
- the result of 4.2.18 with only 34bfcc7 reverted;
AdaptiveByteBufAllocator.metric().usedDirectMemory() and the JDK direct BufferPoolMXBean at the moment of failure.
zephyr45 commented on Nov 17, 2026 I see this issue is already under active investigation. I’d be happy to help reproduce the burst/idle-cache behavior and collect the requested metrics, including the 34bfcc7 comparison, AdaptiveByteBufAllocator.metric().usedDirectMemory(), and JDK direct BufferPoolMXBean values. If that would be useful, may I investigate this and report my findings here ? franz1981 commented on Nov 18, 2026 yes please @zephyr45 any help is welcome ❤️
Consider that we are going through a big change providing a new experimental allocator in our netty modules (the announce once the PR is ready and cooked ;)) which should solve this and many other problems related memory limits/retention tarakeshpolakolu commented on Nov 18, 2026 Thanks @zephyr45. I’m currently going through our internal approval process to download the relevant Netty modules from GitHub. It may take some time, as this is part of the ongoing analysis. Once approved, I’ll reproduce the behavior, collect the requested metrics, and share my findings here. zephyr45 commented on Nov 18, 2026 @franz1981 here's the data you asked for, from a standalone reproducer: https://gist.github.com/zephyr45/61964757eb9c442259a840328a013701
I reproduced excess idle buddy-cache retention after a burst, and the reduced headroom it leaves for later small-buffer allocations. My synthetic single-size and mixed-size ramps did not reproduce the reporter's lower-concurrency result: they recorded more live data at allocation failure on 4.2.18, the same direction as your ~224 vs ~128 MiB. These tests do not model the DataStax/TLS workload.
Your three asks
- 4.2.18 vs 34bfcc7 reverted: tables below. 4.1.138 is included as a second reference.
- Metrics at failure: at every allocation failure,
usedDirectMemory() and the JDK direct pool both read 252–257 MiB, so in this harness the allocator accounts for the whole 256 MiB budget. What differs is how much of it was live data: 67–78% on 4.2.18 vs 33–47% with the old policy. The two metrics agreed within 1 MiB in all 1015 snapshots.
- JFR: every 1 MiB buddy chunk was
pooled=true threadLocal=false. The only other chunks were 524288-byte pooled size-class chunks (from the 16 KB phase) and a few unpooled one-offs, also threadLocal=false. Normalized counts are in the JFR section below.
Retention after a burst (same-thread, median of 3). After a burst completed by all three variants, 4.2.18 retained a median of 65 idle buddy chunks versus 32. That left approximately 24–28 MiB less capacity for subsequent 16 KB buffers (median 164.5 MiB vs 188.3 and 192.6 MiB).
| variant |
idle buddy chunks after the burst (target 32) |
direct after 2 s idle |
16 KB data that still fits |
| 4.2.18 |
65 |
87 MiB |
165 MiB |
| 4.2.18, 34bfcc7 reverted |
32 |
63 MiB |
188 MiB |
| 4.1.138 |
32 |
63 MiB |
193 MiB |
Burst: 512 x 100 KB in flight for 3 s, all released at once, 2 s idle, then 16 KB buffers until Cannot reserve.
- Cross-thread: 4.2.18 also kept more idle chunks (41 vs 30), but its headroom was within a few MiB of both references (195 vs 197 and 194 MiB). So the headroom gap is not consistent across modes.
- At 1024 in flight: 4.2.18 kept 128–136 idle chunks and then fit 100 MiB of 16 KB buffers. The other two variants aborted that burst (
Cannot reserve on 31–32 of 32 workers, in 3/3 runs), so the 1024 case doesn't isolate the cost of retention.
Observed live bytes when allocation failed (ramp: +128 buffers every 2 s until the first Cannot reserve; median of 3)
| variant |
same-thread |
same-thread, 1 s pauses |
cross-thread |
cross-thread, 1 s pauses |
alternating 100 KB / 16 KB |
50/50 mix |
| 4.2.18 |
175 MiB |
172 MiB |
199 MiB |
199 MiB |
147 MiB |
177 MiB |
| 4.2.18, 34bfcc7 reverted |
84 MiB |
84 MiB |
115 MiB |
119 MiB |
83 MiB |
91 MiB |
| 4.1.138 |
84 MiB |
84 MiB |
118 MiB |
115 MiB |
83 MiB |
88 MiB |
That is 1.7–2.1x more on 4.2.18 for single-size load and 1.8–2.0x for mixed sizes. Every failure, in every variant, was a 1 MiB buddy-chunk allocation. This is the live data present when an allocation failed in this harness; it doesn't establish maximum sustained concurrency on real driver event loops.
Method
- JDK 21.0.11 on Windows 11,
-XX:ActiveProcessorCount=16, -XX:MaxDirectMemorySize=256m, -Dio.netty.allocator.type=adaptive, ByteBufAllocator.DEFAULT, leak detection off.
- The reverted variant is 4.2.18.Final with the pre-Fix buddy cache evicting chunks with live buffers #17154 eviction loop restored in a patched
AdaptivePoolingAllocator, placed ahead on the classpath (confirmed with -Xlog:class+load).
- 32 workers, each a single-threaded executor on a
FastThreadLocalThread, created once and reused for every ramp step and both burst phases. Every run checks that each worker id stayed on the same thread.
- Each worker keeps N/32 slots filled and keeps replacing a random slot: release the old buffer, allocate a new one.
- Cross-thread: the old buffer is handed to the next worker, which releases it. It counts against the allocating worker's in-flight budget until released.
- Ramp: starts at 128 in flight, +128 every 2 s. Without pauses, buffers carry over between steps. With pauses, everything is released and the workers idle 1000 ms.
- Mixed sizes: "alternating" allocates only 100 KB in the first half of each step and only 16 KB in the second; "50/50" picks 16 KB or 100 KB at random per allocation.
- Live bytes are counted in the harness. Capacity is read before release, and every run checks the counter returns to 0 after cleanup.
- The idle-chunk count is read reflectively from the buddy cache (
refCnt == 1).
Burst at 512, cross-thread, and peak use during the burst
| variant |
peak direct during burst (same / cross) |
direct after 2 s idle (same / cross) |
idle buddy chunks (same / cross) |
16 KB data that still fits (same / cross) |
| 4.2.18 |
91 / 69 MiB |
87 / 61 MiB |
65 / 41 |
165 / 195 MiB |
| 4.2.18, reverted |
154 / 107 MiB |
63 / 54 MiB |
32 / 30 |
188 / 197 MiB |
| 4.1.138 |
145 / 102 MiB |
63 / 59 MiB |
32 / 30 |
193 / 194 MiB |
JFR chunk events
Burst at 512, median of 3, range in brackets. The burst is timed (3 s) and the variants completed different numbers of buffer allocations, so chunk events are normalized per million completed buffer allocations. Every FreeChunk was paired with its AllocateChunk by address (no unmatched frees, no zero addresses). 4.1.138 doesn't emit these events.
1 MiB AllocateChunk (pooled, not thread-local) per million buffer allocations |
4.2.18 |
4.2.18, reverted |
| same-thread |
498 (439–587) |
11102 (9375–11981) |
| cross-thread |
512 (476–563) |
3476 (3335–3987) |
In all 18 recorded runs, the outstanding capacity from JFR matched usedDirectMemory() exactly. That corroborates the totals; the split into idle and in-use chunks comes from the reflective count.
Linux (Docker)
eclipse-temurin:21-jdk (21.0.13), Linux 5.15 (WSL2), same flags, but the VM has 6 real CPUs. Same-thread, median of 3.
| variant |
ramp: live bytes when allocation failed |
burst 512: idle chunks / 16 KB that fits |
burst 1024: idle chunks / 16 KB that fits |
| 4.2.18 |
195 MiB |
61 / 164 MiB |
125 / 103 MiB |
| 4.2.18, reverted |
82 MiB |
32 / 184 MiB |
burst aborted 3/3; 32 / 213 MiB |
| 4.1.138 |
82 MiB |
32 / 188 MiB |
burst aborted 3/3; 32 / 213 MiB |
Reading of the data
- This matches your narrower reading.
ConcurrentSkipListChunkCache frees chunks only in offerChunk (and on shutdown), and only those idle at that moment, so chunks that go idle while cached stay above the target.
- The ramp pauses show the same retention. During each 1 s pause, with no live data, 4.2.18 held up to 222–231 idle buddy chunks (244–250 MiB direct), against 32 (64 MiB) for the other two. Its ramp result was still within one step of the result without pauses, because the next step's 100 KB buffers reuse those chunks.
- Alternating sizes reduced observed live bytes at failure from 175 to 147 MiB; the mechanism behind this difference has not been isolated.
On a fix, this agrees with your conclusion that idle chunks need trimming even when no further offers arrive. One detail in case it helps: a release-side trim would have to cope with stale cache keys, because chunks are keyed by remainingCapacity at offer time and pollChunk re-keys them. Re-applying #17154 on 4.1 probably should wait for that fix, so 4.1 doesn't pick up the same retention.
Happy to run this harness against the new allocator once its PR is up.
@tarakeshpolakolu your setup is the one that can settle the concurrency question. Once your approval comes through, the most useful data from the real driver + TLS workload would be:
usedDirectMemory() and the JDK direct pool at the moment of failure;
- the JFR chunk events;
- ideally, the same run with 34bfcc7 reverted.
The gist's revert-34bfcc7d.patch and setup.sh show how to build that variant without a Netty build. Whether load drops between your concurrency steps would also help: in these tests, retention only cost capacity when the next load didn't reuse the cached chunks. franz1981 commented on Nov 18, 2026 Just one qq @tarakeshpolakolu did you measured the RSS instead of used memory?
Because from my understanding, in absence of native memory trimming offered by the JVM the tradeoff is:
- 4.2.18: greater throughput without churning and eventually, less changes of native memory fragmentation (which reallly depends by how many threads and size of allocations are performed)
- 4.2.18, 34bfcc7 reverted: much less throughput during the churning and more chance memory got fragmented or allocated (if in new threads) in new glibc thread local arenas, increasing RSS too
So the real fix would be....something I will send a PR in a few, but sadly in the form of a incubating allocator people can give it a shot.
If you are curious I can send the PR and link it here so you can give it a shot with your prod workload and you should have the best of the 2 worlds, but is a redesign on how adaptive work (and there's no buddy cache too ^^). tarakeshpolakolu commented on Nov 19, 2026 Thanks @franz1981 No, I didn't measured RSS.
I'm interested in trying the incubating allocator. Please link the PR.I'll run it against our workload and report back whether it works for us.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|