Netty 中内存分配有一个最顶层的抽象就是ByteBufAllocator,负责分配所有ByteBuf 类型的内存。功能其实不是很多,主要有以下几个重要的API:

public interface ByteBufAllocator {

    /**
     * 分配一块内存,是分配堆外内存或者堆内内存取决于具体实现。
     * Allocate a ByteBuf. If it is a direct or heap buffer depends on the actual implementation .
     */
    ByteBuf buffer();

    /**
     * 尽可能地分配一块堆外直接内存,如果系统不支持则分配堆内内存。适合 I/O
     * Allocate a ByteBuf, preferably a direct buffer which is suitable for I/O.
     */
    ByteBuf ioBuffer();

    /**
     * 分配一块堆内内存。
     * Allocate a heap ByteBuf.
     */
    ByteBuf heapBuffer();
    
    /**
     * 分配一块堆外内存。
     *  Allocate a direct ByteBuf.
     */
    ByteBuf directBuffer();

    /**
     * 组合分配,把多个ByteBuf 组合到一起变成一个整体。
     *  Allocate a CompositeByteBuf. If it is a direct or heap buffer depends on the actual implementation.
     */
    CompositeByteBuf compositeBuffer();
}

本文来源:
Netty内存管理器ByteBufAllocator及内存分配

标签: none

评论已关闭