Netty学习之旅------源码分析Netty线程本地分配机制与PooledByteBuf线程级对象池原理分析

《一线大厂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

  • 14
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值