[Netty源码分析]ByteBuf(五)

ByteBuf的释放

//AbstractReferenceCountedByteBuf
@Override
public final boolean release() {
    for (;;) {
        int refCnt = this.refCnt;
        if (refCnt == 0) {
            throw new IllegalReferenceCountException(0, -1);
        }
        if (refCntUpdater.compareAndSet(this, refCnt, refCnt - 1)) {//判断ByteBuf是否被引用
            if (refCnt == 1) {
                deallocate();
                return true;
            }
            return false;
        }
    }
}
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//PooledByteBuf
@Override
protected final void deallocate() {
    if (handle >= 0) {
        final long handle = this.handle;
        this.handle = -1;//使PooledByteBuf不指向任何一块内存
        memory = null;
        tmpNioBuf = null;
        chunk.arena.free(chunk, handle, maxLength, cache);//释放内存
        chunk = null;
        recycle();//回收对象,加到对象池
    }
}
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//PooledArena
1. 连续的内存区段加到缓存
void free(PoolChunk<T> chunk, long handle, int normCapacity, PoolThreadCache cache) {
    if (chunk.unpooled) {
        int size = chunk.chunkSize();
        destroyChunk(chunk);
        activeBytesHuge.add(-size);
        deallocationsHuge.increment();
    } else {
        SizeClass sizeClass = sizeClass(normCapacity);
        if (cache != null && cache.add(this, chunk, handle, normCapacity, sizeClass)) {
            // cached so not free it.
            return;
        }
        freeChunk(chunk, handle, sizeClass);
    }
}
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//PooledThreadCache
boolean add(PoolArena<?> area, PoolChunk chunk, long handle, int normCapacity, SizeClass sizeClass) {
    MemoryRegionCache<?> cache = cache(area, normCapacity, sizeClass);
    if (cache == null) {
        return false;
    }
    return cache.add(chunk, handle);
}
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//PooledThreadCache
private MemoryRegionCache<?> cache(PoolArena<?> area, int normCapacity, SizeClass sizeClass) {
    //拿到第几个cache进行返回
    switch (sizeClass) {
      case Normal:
          return cacheForNormal(area, normCapacity);
      case Small:
          return cacheForSmall(area, normCapacity);
      case Tiny:
          return cacheForTiny(area, normCapacity);
      default:
          throw new Error();
    }
}
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//PooledThreadCache
private static <T> MemoryRegionCache<T> cache(MemoryRegionCache<T>[] cache, int idx) {
    if (cache == null || idx > cache.length - 1) {
        return null;
    }
    return cache[idx];
}
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//PooledThreadCache
public final boolean add(PoolChunk<T> chunk, long handle) {
    Entry<T> entry = newEntry(chunk, handle);
    boolean queued = queue.offer(entry);
    if (!queued) {
        // If it was not possible to cache the chunk, immediately recycle the entry
        entry.recycle();
    }
    return queued;
 }
2. 标志连续的内存区段为未使用(无法加入cache情况下)
//PoolArena
void freeChunk(PoolChunk<T> chunk, long handle, SizeClass sizeClass) {
    final boolean destroyChunk;
    synchronized (this) {
        switch (sizeClass) {
          case Normal:
              ++deallocationsNormal;
              break;
          case Small:
              ++deallocationsSmall;
              break;
          case Tiny:
              ++deallocationsTiny;
              break;
          default:
              throw new Error();
        }
        destroyChunk = !chunk.parent.free(chunk, handle);
    }
    if (destroyChunk) {
        // destroyChunk not need to be called while holding the synchronized lock.
        destroyChunk(chunk);
    }
}
//PoolChunkList
private boolean move(PoolChunk<T> chunk) {
    assert chunk.usage() < maxUsage;
    if (chunk.usage() < minUsage) {
        // Move the PoolChunk down the PoolChunkList linked-list.
        return move0(chunk);
    }
    // PoolChunk fits into this PoolChunkList, adding it here.
    add0(chunk);
    return true;
}
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//PoolChunk
/**
     * Free a subpage or a run of pages
     * When a subpage is freed from PoolSubpage, it might be added back to subpage pool of the owning PoolArena
     * If the subpage pool in PoolArena has at least one other PoolSubpage of given elemSize, we can
     * completely free the owning Page so it is available for subsequent allocations
     *
     * @param handle handle to free
     */
void free(long handle) {
    int memoryMapIdx = memoryMapIdx(handle);
    int bitmapIdx = bitmapIdx(handle);
    if (bitmapIdx != 0) { // free a subpage
        PoolSubpage<T> subpage = subpages[subpageIdx(memoryMapIdx)];
        assert subpage != null && subpage.doNotDestroy;
        // Obtain the head of the PoolSubPage pool that is owned by the PoolArena and synchronize on it.
        // This is need as we may add it back and so alter the linked-list structure.
        PoolSubpage<T> head = arena.findSubpagePoolHead(subpage.elemSize);
        synchronized (head) {
            if (subpage.free(head, bitmapIdx & 0x3FFFFFFF)) {
                return;
            }
        }
    }
    freeBytes += runLength(memoryMapIdx);
    setValue(memoryMapIdx, depth(memoryMapIdx));
    updateParentsFree(memoryMapIdx);
 }
3. 回收对象到对象池
//PoolByteBuf
private void recycle() {
    recyclerHandle.recycle(this);
}
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//Recycler$DefaultHandle
//回收到handle的stack中
@Override
public void recycle(Object object) {
    if (object != value) {
        throw new IllegalArgumentException("object does not belong to handle");
    }
    stack.push(this);
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容