netty-池化內存-PooledByteBufAllocator

PooledByteBufAllocator

屬生

    public static final PooledByteBufAllocator DEFAULT =
            new PooledByteBufAllocator(PlatformDependent.directBufferPreferred());

    private final PoolArena<byte[]>[] heapArenas; //一般是2倍CPU核心數
    private final PoolArena<ByteBuffer>[] directArenas;//一般是2倍CPU核心數
    private final int tinyCacheSize;//512
    private final int smallCacheSize;//256
    private final int normalCacheSize;//64
    private final List<PoolArenaMetric> heapArenaMetrics;
    private final List<PoolArenaMetric> directArenaMetrics;
    private final PoolThreadLocalCache threadCache;//線程局部變量
    private final int chunkSize;//PoolChunk 的總內存
    private final PooledByteBufAllocatorMetric metric;

內存分配

    @Override
    protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) {
        PoolThreadCache cache = threadCache.get();//從線程局部變量獲取一個,每個線程局部變量獲取的對象不一樣
        PoolArena<ByteBuffer> directArena = cache.directArena;//基本上每個核心的值不一樣

        final ByteBuf buf;
        if (directArena != null) {
            buf = directArena.allocate(cache, initialCapacity, maxCapacity);//分配
        } else {
            buf = PlatformDependent.hasUnsafe() ?
                    UnsafeByteBufUtil.newUnsafeDirectByteBuf(this, initialCapacity, maxCapacity) :
                    new UnpooledDirectByteBuf(this, initialCapacity, maxCapacity);
        }

        return toLeakAwareBuffer(buf);//追蹤內存泄漏
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章