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 — Aggressively remove PoolThreadCache references from its finalizer object · netty/netty@f618686

File tree

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

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/PoolThreadCache.java
+17-21Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,18 @@ void free(boolean finalizer) {
205205
// As free() may be called either by the finalizer or by FastThreadLocal.onRemoval(...) we need to ensure
206206
// we only call this one time.
207207
if (freed.compareAndSet(false, true)) {
208+
if (freeOnFinalize != null) {
209+
// Help GC: this can race with a finalizer thread, but will be null out regardless
210+
freeOnFinalize.cache = null;
211+
}
208212
int numFreed = free(smallSubPageDirectCaches, finalizer) +
209-
free(normalDirectCaches, finalizer) +
210-
free(smallSubPageHeapCaches, finalizer) +
211-
free(normalHeapCaches, finalizer);
213+
free(normalDirectCaches, finalizer) +
214+
free(smallSubPageHeapCaches, finalizer) +
215+
free(normalHeapCaches, finalizer);
212216

213217
if (numFreed > 0 && logger.isDebugEnabled()) {
214218
logger.debug("Freed {} thread-local buffer(s) from thread: {}", numFreed,
215-
Thread.currentThread().getName());
219+
Thread.currentThread().getName());
216220
}
217221

218222
if (directArena != null) {
@@ -222,21 +226,6 @@ void free(boolean finalizer) {
222226
if (heapArena != null) {
223227
heapArena.numThreadCaches.getAndDecrement();
224228
}
225-
} else {
226-
// See https://github.com/netty/netty/issues/12749
227-
checkCacheMayLeak(smallSubPageDirectCaches, "SmallSubPageDirectCaches");
228-
checkCacheMayLeak(normalDirectCaches, "NormalDirectCaches");
229-
checkCacheMayLeak(smallSubPageHeapCaches, "SmallSubPageHeapCaches");
230-
checkCacheMayLeak(normalHeapCaches, "NormalHeapCaches");
231-
}
232-
}
233-
234-
private static void checkCacheMayLeak(MemoryRegionCache<?>[] caches, String type) {
235-
for (MemoryRegionCache<?> cache : caches) {
236-
if (!cache.queue.isEmpty()) {
237-
logger.debug("{} memory may leak.", type);
238-
return;
239-
}
240229
}
241230
}
242231

@@ -484,7 +473,8 @@ public Entry newObject(Handle<Entry> handle) {
484473
}
485474

486475
private static final class FreeOnFinalize {
487-
private final PoolThreadCache cache;
476+
477+
private volatile PoolThreadCache cache;
488478

489479
private FreeOnFinalize(PoolThreadCache cache) {
490480
this.cache = cache;
@@ -497,7 +487,13 @@ protected void finalize() throws Throwable {
497487
try {
498488
super.finalize();
499489
} finally {
500-
cache.free(true);
490+
PoolThreadCache cache = this.cache;
491+
// this can race with a non-finalizer thread calling free: regardless who wins, the cache will be
492+
// null out
493+
this.cache = null;
494+
if (cache != null) {
495+
cache.free(true);
496+
}
501497
}
502498
}
503499
}
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java
+23-1Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator implements
5151
private static final boolean DEFAULT_USE_CACHE_FOR_ALL_THREADS;
5252
private static final int DEFAULT_DIRECT_MEMORY_CACHE_ALIGNMENT;
5353
static final int DEFAULT_MAX_CACHED_BYTEBUFFERS_PER_CHUNK;
54+
private static final boolean DEFAULT_DISABLE_CACHE_FINALIZERS_FOR_FAST_THREAD_LOCAL_THREADS;
5455

5556
private static final int MIN_PAGE_SIZE = 4096;
5657
private static final int MAX_CHUNK_SIZE = (int) (((long) Integer.MAX_VALUE + 1) / 2);
@@ -148,6 +149,9 @@ public void run() {
148149
DEFAULT_USE_CACHE_FOR_ALL_THREADS = SystemPropertyUtil.getBoolean(
149150
"io.netty.allocator.useCacheForAllThreads", false);
150151

152+
DEFAULT_DISABLE_CACHE_FINALIZERS_FOR_FAST_THREAD_LOCAL_THREADS = SystemPropertyUtil.getBoolean(
153+
"io.netty.allocator.disableCacheFinalizersForFastThreadLocalThreads", false);
154+
151155
// Use 1023 by default as we use an ArrayDeque as backing storage which will then allocate an internal array
152156
// of 1024 elements. Otherwise we would allocate 2048 and only use 1024 which is wasteful.
153157
DEFAULT_MAX_CACHED_BYTEBUFFERS_PER_CHUNK = SystemPropertyUtil.getInt(
@@ -175,6 +179,8 @@ public void run() {
175179
logger.debug("-Dio.netty.allocator.useCacheForAllThreads: {}", DEFAULT_USE_CACHE_FOR_ALL_THREADS);
176180
logger.debug("-Dio.netty.allocator.maxCachedByteBuffersPerChunk: {}",
177181
DEFAULT_MAX_CACHED_BYTEBUFFERS_PER_CHUNK);
182+
logger.debug("-Dio.netty.allocator.disableCacheFinalizersForFastThreadLocalThreads: {}",
183+
DEFAULT_DISABLE_CACHE_FINALIZERS_FOR_FAST_THREAD_LOCAL_THREADS);
178184
}
179185
}
180186

@@ -432,6 +438,14 @@ public static int defaultMaxOrder() {
432438
return DEFAULT_MAX_ORDER;
433439
}
434440

441+
/**
442+
* Default control creation of PoolThreadCache finalizers for FastThreadLocalThreads -
443+
* System Property: io.netty.allocator.disableCacheFinalizersForFastThreadLocalThreads - default false
444+
*/
445+
public static boolean defaultDisableCacheFinalizersForFastThreadLocalThreads() {
446+
return DEFAULT_DISABLE_CACHE_FINALIZERS_FOR_FAST_THREAD_LOCAL_THREADS;
447+
}
448+
435449
/**
436450
* Default thread caching behavior - System Property: io.netty.allocator.useCacheForAllThreads - default false
437451
*/
@@ -524,7 +538,7 @@ protected synchronized PoolThreadCache initialValue() {
524538
executor != null) {
525539
final PoolThreadCache cache = new PoolThreadCache(
526540
heapArena, directArena, smallCacheSize, normalCacheSize,
527-
DEFAULT_MAX_CACHED_BUFFER_CAPACITY, DEFAULT_CACHE_TRIM_INTERVAL, true);
541+
DEFAULT_MAX_CACHED_BUFFER_CAPACITY, DEFAULT_CACHE_TRIM_INTERVAL, useCacheFinalizers(current));
528542

529543
if (DEFAULT_CACHE_TRIM_INTERVAL_MILLIS > 0) {
530544
if (executor != null) {
@@ -565,6 +579,14 @@ private <T> PoolArena<T> leastUsedArena(PoolArena<T>[] arenas) {
565579
}
566580
}
567581

582+
private static boolean useCacheFinalizers(Thread current) {
583+
if (!defaultDisableCacheFinalizersForFastThreadLocalThreads()) {
584+
return true;
585+
}
586+
return current instanceof FastThreadLocalThread &&
587+
((FastThreadLocalThread) current).willCleanupFastThreadLocals();
588+
}
589+
568590
@Override
569591
public PooledByteBufAllocatorMetric metric() {
570592
return metric;

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.