GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix direct memory OOM on low-core containers by franz1981 · Pull Request #17166 · netty/netty

Fix direct memory OOM on low-core containers #17166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
chrisvest merged 3 commits into netty:4.2 from franz1981:lowmem-toggle
Aug 2, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Fix direct memory OOM on low-core containers #17166

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
1b83f89
Fix direct memory OOM on low-core containers
franz1981 Jul 29, 2026
caede36
Bound ThreadLocalSizeClassedChunkCache to cap direct memory retention
franz1981 Jul 31, 2026
9832f68
Small test fixes
chrisvest Aug 2, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
Fix direct memory OOM on low-core containers
Motivation:

PR #16766 introduced SHARED_CACHE_CAPACITY = max(128, CHUNK_REUSE_QUEUE * 2)
to prevent chunk churn. On 1-core containers where CHUNK_REUSE_QUEUE is 2,
the 128-slot floor allows each of the 16 size classes to retain up to 128
chunks of ~128 KB, totalling 256 MB of direct memory in caches alone.

On JDK 21 without -Dio.netty.tryReflectionSetAccessible=true, Netty
allocates direct chunks via ByteBuffer.allocateDirect(), tracked against
MaxDirectMemorySize. The cache capacity can exhaust the entire direct
memory budget, causing OOM as reported in #17107.

Additionally, IS_LOW_MEM (triggered when Runtime.maxMemory() <= 512 MB)
uses heap budget as a proxy for native memory availability, which is wrong
for off-heap workloads. There is no override for users who know their
environment better than the heuristic.

Modification:

- Reduce the SHARED_CACHE_CAPACITY floor from 128 to 16.
- Add io.netty.allocator.lowMemory system property to override IS_LOW_MEM
  auto-detection, following the precedent of the existing
  disableThreadLocalMagazinesOnLowMemory property.
- Fix cacheSettlesAfterBurstShared test to cap excess chunks within
  SHARED_CACHE_CAPACITY, preventing silent drops on the bounded MPMC queue
  when CHUNK_REUSE_QUEUE is large relative to the capacity floor.

Result:

Worst-case cache retention drops from 256 MB to 32 MB on 1-4 core machines.
The CHUNK_REUSE_QUEUE * 2 formula still governs on 8+ cores (same as
before). The floor of 16 is 8x the pre-#16766 capacity on 1-core, so
the original chunk churn problem does not regress. Users can force
IS_LOW_MEM on or off via -Dio.netty.allocator.lowMemory.
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@franz1981
franz1981 committed Jul 31, 2026
commit 1b83f89d8d29ef3b6e3cdfceb0804c17b6e22f19

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

6 changes: 4 additions & 2 deletions 6 buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@
@UnstableApi
final class AdaptivePoolingAllocator {
private static final int LOW_MEM_THRESHOLD = 512 * 1024 * 1024;
private static final boolean IS_LOW_MEM = Runtime.getRuntime().maxMemory() <= LOW_MEM_THRESHOLD;
private static final boolean IS_LOW_MEM = SystemPropertyUtil.getBoolean(
"io.netty.allocator.lowMemory",
Runtime.getRuntime().maxMemory() <= LOW_MEM_THRESHOLD);

/**
* Whether the IS_LOW_MEM setting should disable thread-local magazines.
Expand Down Expand Up @@ -884,7 +886,7 @@ public boolean isEmpty() {
static final class SharedSizeClassedChunkCache extends SizeClassedChunkCache {
// Must exceed CHUNK_REUSE_QUEUE (the retention floor) to leave room for burst absorption.
// TODO replace with an unbounded concurrent collection once available.
private static final int SHARED_CACHE_CAPACITY = Math.max(128, CHUNK_REUSE_QUEUE * 2);
private static final int SHARED_CACHE_CAPACITY = Math.max(16, CHUNK_REUSE_QUEUE * 2);
private final Queue<SizeClassedChunk> queue;
private final AtomicLong purgeBudget;
private final ArrayList<SizeClassedChunk> deferredBuffer = new ArrayList<>();
Expand Down
9 changes: 6 additions & 3 deletions 9 buffer/src/test/java/io/netty/buffer/SizeClassedChunkCacheTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -352,16 +352,19 @@ void cacheSettlesAtRetentionFloorAfterBurstThreadLocal() {
void cacheSettlesAfterBurstShared() {
SizeClassedChunkCache cache = SizeClassedChunkCache.create(false);

int excess = 10;
int crq = AdaptivePoolingAllocator.CHUNK_REUSE_QUEUE;
int capacity = Math.max(16, crq * 2);
int excess = Math.min(10, capacity - crq);
assertTrue(excess > 0, "excess must be positive for the test to be meaningful");
SizeClassedChunk workingSet = chunkWithCapacity();
cache.offerChunk(workingSet);
for (int i = 0; i < AdaptivePoolingAllocator.CHUNK_REUSE_QUEUE - 1; i++) {
for (int i = 0; i < crq - 1; i++) {
cache.offerChunk(chunkWithoutCapacity());
}
SizeClassedChunk[] excessChunks = new SizeClassedChunk[excess];
for (int i = 0; i < excess; i++) {
excessChunks[i] = fullChunk();
cache.offerChunk(excessChunks[i]);
assertTrue(cache.offerChunk(excessChunks[i]), "all excess chunks must be accepted by the queue");
}

int maxCycles = (AdaptivePoolingAllocator.CHUNK_PURGE_THRESHOLD + 1) * 3;
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.