《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
-
@param directArena 线程使用的PoolArena.DirectArena
-
@param tinyCacheSize, tiny内存缓存的个数。默认为512
-
@param smallCacheSize small内存缓存的个数,默认为256个
-
@param normalCacheSize normalCacheSize缓存的个数,默认为64
-
@param maxCacheBufferCapacity
-
normalHeapCaches中单个缓存区域的最大大小,默认为32k 也就是normalHeapCaches[length-1]中缓存的最大内存空间
-
@param freeSweepAllocationThreshold 在本地线程每分配freeSweepAllocationThreshold 次内存后,检测一下是否需要释放内存。
*/
PoolThreadCache(PoolArena<byte[]> heapArena, PoolArena directArena,
int tinyCacheSize, int smallCacheSize, int normalCacheSize,
int maxCachedBufferCapacity, int freeSweepAllocationThreshold) {
if (maxCachedBufferCapacity < 0) {
throw new IllegalArgumentException("maxCachedBufferCapacity: "
- maxCachedBufferCapacity + " (expected: >= 0)");
}
if (freeSweepAllocationThreshold < 1) {
throw new IllegalArgumentException("freeSweepAllocationThreshold: "
- maxCachedBufferCapacity + " (expected: > 0)");
}
this.freeSweepAllocationThreshold = freeSweepAllocationThreshold;
this.heapArena = heapArena;
this.directArena = directArena;
if (directArena != null) {
tinySubPageDirectCaches = createSubPageCaches(tinyCacheSize, PoolArena.numTinySubpagePools);
smallSubPageDirectCaches = createSubPageCaches(smallCacheSize, directArena.numSmallSubpagePools);
numShiftsNormalDirect = log2(directArena.pageSize);
normalDirectCaches = createNormalCaches(
normalCacheSize, maxCachedBufferCapacity, directArena);
} else {
// No directArea is configured so just null out all caches
tinySubPageDirectCaches = null;
smallSubPageDirectCaches = null;
normalDirectCaches = null;
numShiftsNormalDirect = -1;
}
if (heapArena != null) {
// Create the caches for the heap allocations
tinySubPageHeapCaches = createSubPageCaches(tinyCacheSize, PoolArena.numTinySubpagePools);
smallSubPageHeapCaches = createSubPageCaches(smallCacheSize, heapArena.numSmallSubpagePools);
numShiftsNormalHeap = log2(heapArena.pageSize);
normalHeapCaches = createNormalCaches(
normalCacheSize, maxCachedBufferCapacity, heapArena); //@1
} else {
// No heapArea is configured so just null out all caches
tinySubPageHeapCaches = null;
smallSubPageHeapCaches = null;
normalHeapCaches = null;
numShiftsNormalHeap = -1;
}
// The thread-local cache will keep a list of pooled buffers which must be returned to
// the pool when the thread is not alive anymore.
ThreadDeathWatcher.watch(thread, freeTask);
}
在方法前,已经对构造方法的入参加了说明,关注如下两个方法。
代码@1:创建createNormalCaches 。
由于PoolThreadCache的设计理念与PoolArena一样,本身并不涉及到具体内存的存储,PoolThreadCache内部维护MemoryRegionCache[] tinySubpageHeapCaches,MemoryRegionCache[] smallSubpageHeapCaches,其数组长度与PoolArena相同,MemoryRegionCaches[] normalHeapCaches,缓存的是noraml内存,Netty把大于pageSize小于chunkSize的空间成为normal内存。normalHeapCaches[1] 是normalHeapCaches[0] 的2倍, 先重点关注PoolThreadCache createNormalCaches 源码:
private static NormalMemoryRegionCache[] createNormalCaches(
int cacheSize, int maxCachedBufferCapacity, PoolArena area) {
if (cacheSize > 0) {
int max = Math.min(area.chunkSize, maxCachedBufferCapacity); //@1
int arraySi