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 (#17166) · netty/netty@6b02a3e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src
    • main/java/io/netty/buffer
      • AdaptivePoolingAllocator.java
    • test/java/io/netty/buffer
      • SizeClassedChunkCacheTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java‎

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
+42-9Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@
8484
@UnstableApi
8585
final class AdaptivePoolingAllocator {
8686
private static final int LOW_MEM_THRESHOLD = 512 * 1024 * 1024;
87-
private static final boolean IS_LOW_MEM = Runtime.getRuntime().maxMemory() <= LOW_MEM_THRESHOLD;
87+
private static final boolean IS_LOW_MEM = SystemPropertyUtil.getBoolean(
88+
"io.netty.allocator.lowMemory",
89+
Runtime.getRuntime().maxMemory() <= LOW_MEM_THRESHOLD);
8890

8991
/**
9092
* Whether the IS_LOW_MEM setting should disable thread-local magazines.
@@ -134,6 +136,27 @@ final class AdaptivePoolingAllocator {
134136
static final int CHUNK_PURGE_THRESHOLD = Math.max(1, SystemPropertyUtil.getInt(
135137
"io.netty.allocator.chunkPurgeThreshold", 3));
136138

139+
/**
140+
* Per-size-class upper bound (in bytes) on the thread-local chunk cache.
141+
* When a size class cache holds this many bytes worth of chunks,
142+
* further offers are rejected and the chunk is marked for immediate deallocation.
143+
* Chunks already in the cache are only evicted by the purge mechanism (they must be full and idle for
144+
* {@link #CHUNK_PURGE_THRESHOLD} consecutive purge cycles).
145+
*/
146+
static final int THREAD_LOCAL_CACHE_MAX_BYTES = Math.max(1, SystemPropertyUtil.getInt(
147+
"io.netty.allocator.threadLocalChunkCacheMaxBytes", 8 * 1024 * 1024));
148+
149+
/**
150+
* Per-size-class lower bound (in bytes) on the thread-local chunk cache.
151+
* The purge mechanism will not evict chunks below this retention floor, even if they are full and idle.
152+
* Clamped to {@link #THREAD_LOCAL_CACHE_MAX_BYTES} if the configured value exceeds it.
153+
* When equal to {@link #THREAD_LOCAL_CACHE_MAX_BYTES}, purge eviction is effectively disabled.
154+
*/
155+
static final int THREAD_LOCAL_CACHE_MIN_BYTES = Math.min(THREAD_LOCAL_CACHE_MAX_BYTES,
156+
Math.max(1, SystemPropertyUtil.getInt(
157+
"io.netty.allocator.threadLocalChunkCacheMinBytes",
158+
THREAD_LOCAL_CACHE_MAX_BYTES / 2)));
159+
137160
/**
138161
* The capacity if the magazine local buffer queue. This queue just pools the outer ByteBuf instance and not
139162
* the actual memory and so helps to reduce GC pressure.
@@ -522,7 +545,8 @@ interface ChunkCache {
522545
// 2. EVICTION: idle chunks with epoch > CHUNK_PURGE_THRESHOLD are evicted (markToDeallocate).
523546
// Eviction is immediate — all segments are in, no outstanding references.
524547
// Non-idle chunks are never evicted (deallocation would be deferred, not immediate).
525-
// At least CHUNK_REUSE_QUEUE chunks are always retained (retention floor).
548+
// A retention floor prevents over-eviction: CHUNK_REUSE_QUEUE for the shared cache,
549+
// purgeRetentionFloor (from THREAD_LOCAL_CACHE_MIN_BYTES) for the thread-local cache.
526550
//
527551
// 3. SCAN RESET: scanForCapacity resets purgeEpoch = 0 on the chunk it picks. The scan
528552
// knows the chunk is being used. The chunk gets allocated from, becomes non-idle, and
@@ -538,8 +562,9 @@ interface ChunkCache {
538562
// Shared: approximate, converges over multiple cycles (FIFO queue ordering,
539563
// LRU preference in scan, retained counter in purge).
540564
abstract static class SizeClassedChunkCache implements ChunkCache {
541-
static SizeClassedChunkCache create(boolean isThreadLocal) {
542-
return isThreadLocal ? new ThreadLocalSizeClassedChunkCache() : new SharedSizeClassedChunkCache();
565+
static SizeClassedChunkCache create(boolean isThreadLocal, int chunkSize) {
566+
return isThreadLocal ? new ThreadLocalSizeClassedChunkCache(chunkSize) :
567+
new SharedSizeClassedChunkCache();
543568
}
544569

545570
@Override
@@ -636,7 +661,7 @@ static SizeClassedChunkCache create(boolean isThreadLocal) {
636661
* notEmptyCount=2, count=6
637662
* </pre>
638663
* Idle chunks ({@code remainingCapacity == capacity}) age via purgeEpoch and are evicted
639-
* past threshold, but at least {@link #CHUNK_REUSE_QUEUE} chunks are always retained.
664+
* past threshold, but at least {@code purgeRetentionFloor} chunks are always retained.
640665
*/
641666
static final class ThreadLocalSizeClassedChunkCache extends SizeClassedChunkCache {
642667
SizeClassedChunk[] chunks; // package-private for testing
@@ -645,10 +670,15 @@ static final class ThreadLocalSizeClassedChunkCache extends SizeClassedChunkCach
645670
int count;
646671
int notEmptyCount;
647672
private long purgeBudget;
673+
final int maxCachedChunks; // package-private for testing
674+
final int purgeRetentionFloor; // package-private for testing
648675

649-
ThreadLocalSizeClassedChunkCache() {
676+
ThreadLocalSizeClassedChunkCache(int chunkSize) {
650677
chunks = new SizeClassedChunk[8];
651678
purgeBudget = CHUNK_PURGE_POLLS_THREAD_LOCAL;
679+
maxCachedChunks = Math.max(1, THREAD_LOCAL_CACHE_MAX_BYTES / chunkSize);
680+
purgeRetentionFloor = Math.min(maxCachedChunks,
681+
Math.max(1, THREAD_LOCAL_CACHE_MIN_BYTES / chunkSize));
652682
}
653683

654684
@Override
@@ -709,7 +739,7 @@ private void runPurgeScan() {
709739
if (chunk.purgeEpoch > 0) {
710740
assert chunk.hasFullCapacity();
711741
chunk.purgeEpoch++;
712-
if (chunk.purgeEpoch > CHUNK_PURGE_THRESHOLD && survivors > CHUNK_REUSE_QUEUE) {
742+
if (chunk.purgeEpoch > CHUNK_PURGE_THRESHOLD && survivors > purgeRetentionFloor) {
713743
chunk.markToDeallocate();
714744
chunks[readIdx] = null;
715745
survivors--;
@@ -787,6 +817,9 @@ private void partition(int size) {
787817

788818
@Override
789819
public boolean offerChunk(Chunk chunk) {
820+
if (count >= maxCachedChunks) {
821+
return false;
822+
}
790823
if (count == chunks.length) {
791824
SizeClassedChunk[] newChunks = new SizeClassedChunk[chunks.length * 2];
792825
for (int i = 0; i < count; i++) {
@@ -884,7 +917,7 @@ public boolean isEmpty() {
884917
static final class SharedSizeClassedChunkCache extends SizeClassedChunkCache {
885918
// Must exceed CHUNK_REUSE_QUEUE (the retention floor) to leave room for burst absorption.
886919
// TODO replace with an unbounded concurrent collection once available.
887-
private static final int SHARED_CACHE_CAPACITY = Math.max(128, CHUNK_REUSE_QUEUE * 2);
920+
private static final int SHARED_CACHE_CAPACITY = Math.max(16, CHUNK_REUSE_QUEUE * 2);
888921
private final Queue<SizeClassedChunk> queue;
889922
private final AtomicLong purgeBudget;
890923
private final ArrayList<SizeClassedChunk> deferredBuffer = new ArrayList<>();
@@ -1163,7 +1196,7 @@ public ChunkController createController(MagazineGroup group) {
11631196

11641197
@Override
11651198
public ChunkCache createChunkCache(boolean isThreadLocal) {
1166-
return SizeClassedChunkCache.create(isThreadLocal);
1199+
return SizeClassedChunkCache.create(isThreadLocal, chunkSize);
11671200
}
11681201
}
11691202

0 commit comments

Comments
0 (0)
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.