PoolThreadCache简单理解

PoolThreadCache用户缓存线程申请的内存,Netty中内存需要从PoolArena中申请,而这些申请到的内存在使用完之后不会立刻还给PoolArena,而是将其缓存到线程局部变量中,PoolThreadCache正是是线程局部变量,其创建基于PoolThreadLocalCachePoolThreadCache作为一个中间层又在一定程度上提高了内存的分配效率。

PoolThreadCache中包含几类属性:

  1. Arena:初始化时与PoolThreadCache关联的Arena。
  2. MemoryRegionCach:使用队列保存缓存的内存,
  3. tinySubPageDirectCaches:MemoryRegionCache的数组,对应不同大小的内存。
添加缓存

调用ByteBufrelease方法释放的时候,如果是池化的ByteBuf那么会将其放入PoolThreadCache中。

boolean add(PoolArena<?> area, PoolChunk chunk, ByteBuffer nioBuffer,
            long handle, int normCapacity, SizeClass sizeClass) {
    // cache方法用于定位MemoryRegionCache
    MemoryRegionCache<?> cache = cache(area, normCapacity, sizeClass);
    if (cache == null) {
        return false;
    }
    return cache.add(chunk, nioBuffer, handle);
}

public final boolean add(PoolChunk<T> chunk, ByteBuffer nioBuffer, long handle) {
    Entry<T> entry = newEntry(chunk, nioBuffer, handle);
    //  ByteBuffer加入到队列中
    boolean queued = queue.offer(entry);
    if (!queued) {
        // If it was not possible to cache the chunk, immediately recycle the entry
        entry.recycle();
    }

    return queued;
}

获取缓存

申请内存时先在PoolThreadCache中找是否有可用缓存,首先根据申请内存的大小定位到MemoryRegionCache,然后从队列中取出一块内存。

boolean allocateTiny(PoolArena<?> area, PooledByteBuf<?> buf, int reqCapacity, int normCapacity) {
    // cacheForTiny 定位MemoryRegionCache
    return allocate(cacheForTiny(area, normCapacity), buf, reqCapacity);
}
private boolean allocate(MemoryRegionCache<?> cache, PooledByteBuf buf, int reqCapacity) {
    if (cache == null) {
        // no cache found so just return false here
        return false;
    }
    // 从队列中取出一个可用的内存
    boolean allocated = cache.allocate(buf, reqCapacity, this);
    if (++ allocations >= freeSweepAllocationThreshold) {
        allocations = 0;
        trim();
    }
    return allocated;
}
public final boolean allocate(PooledByteBuf<T> buf, int reqCapacity, PoolThreadCache threadCache) {
    Entry<T> entry = queue.poll();
    if (entry == null) {
        return false;
    }
    // 对buf进行初始化
    initBuf(entry.chunk, entry.nioBuffer, entry.handle, buf, reqCapacity, threadCache);
    entry.recycle();

    // allocations is not thread-safe which is fine as this is only called from the same thread all time.
    ++ allocations;
    return true;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值