@@ -51,6 +51,7 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator implements |
51 | 51 | private static final boolean DEFAULT_USE_CACHE_FOR_ALL_THREADS; |
52 | 52 | private static final int DEFAULT_DIRECT_MEMORY_CACHE_ALIGNMENT; |
53 | 53 | static final int DEFAULT_MAX_CACHED_BYTEBUFFERS_PER_CHUNK; |
| 54 | + private static final boolean DEFAULT_DISABLE_CACHE_FINALIZERS_FOR_FAST_THREAD_LOCAL_THREADS; |
54 | 55 |
|
55 | 56 | private static final int MIN_PAGE_SIZE = 4096; |
56 | 57 | private static final int MAX_CHUNK_SIZE = (int) (((long) Integer.MAX_VALUE + 1) / 2); |
@@ -148,6 +149,9 @@ public void run() { |
148 | 149 | DEFAULT_USE_CACHE_FOR_ALL_THREADS = SystemPropertyUtil.getBoolean( |
149 | 150 | "io.netty.allocator.useCacheForAllThreads", false); |
150 | 151 |
|
| 152 | + DEFAULT_DISABLE_CACHE_FINALIZERS_FOR_FAST_THREAD_LOCAL_THREADS = SystemPropertyUtil.getBoolean( |
| 153 | + "io.netty.allocator.disableCacheFinalizersForFastThreadLocalThreads", false); |
| 154 | + |
151 | 155 | // Use 1023 by default as we use an ArrayDeque as backing storage which will then allocate an internal array |
152 | 156 | // of 1024 elements. Otherwise we would allocate 2048 and only use 1024 which is wasteful. |
153 | 157 | DEFAULT_MAX_CACHED_BYTEBUFFERS_PER_CHUNK = SystemPropertyUtil.getInt( |
@@ -175,6 +179,8 @@ public void run() { |
175 | 179 | logger.debug("-Dio.netty.allocator.useCacheForAllThreads: {}", DEFAULT_USE_CACHE_FOR_ALL_THREADS); |
176 | 180 | logger.debug("-Dio.netty.allocator.maxCachedByteBuffersPerChunk: {}", |
177 | 181 | DEFAULT_MAX_CACHED_BYTEBUFFERS_PER_CHUNK); |
| 182 | + logger.debug("-Dio.netty.allocator.disableCacheFinalizersForFastThreadLocalThreads: {}", |
| 183 | + DEFAULT_DISABLE_CACHE_FINALIZERS_FOR_FAST_THREAD_LOCAL_THREADS); |
178 | 184 | } |
179 | 185 | } |
180 | 186 |
|
@@ -432,6 +438,14 @@ public static int defaultMaxOrder() { |
432 | 438 | return DEFAULT_MAX_ORDER; |
433 | 439 | } |
434 | 440 |
|
| 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 | + |
435 | 449 | /** |
436 | 450 | * Default thread caching behavior - System Property: io.netty.allocator.useCacheForAllThreads - default false |
437 | 451 | */ |
@@ -524,7 +538,7 @@ protected synchronized PoolThreadCache initialValue() { |
524 | 538 | executor != null) { |
525 | 539 | final PoolThreadCache cache = new PoolThreadCache( |
526 | 540 | 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)); |
528 | 542 |
|
529 | 543 | if (DEFAULT_CACHE_TRIM_INTERVAL_MILLIS > 0) { |
530 | 544 | if (executor != null) { |
@@ -565,6 +579,14 @@ private <T> PoolArena<T> leastUsedArena(PoolArena<T>[] arenas) { |
565 | 579 | } |
566 | 580 | } |
567 | 581 |
|
| 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 | + |
568 | 590 | @Override |
569 | 591 | public PooledByteBufAllocatorMetric metric() { |
570 | 592 | return metric; |
|