Home / Class/ AdaptiveByteBufAllocator Class — netty Architecture

AdaptiveByteBufAllocator Class — netty Architecture

Architecture documentation for the AdaptiveByteBufAllocator class in AdaptiveByteBufAllocator.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  e26ce83f_6a02_6e37_2f80_6f284af38963["AdaptiveByteBufAllocator"]
  6b49ef9c_b171_f31a_8052_bb3056b414c3["AdaptiveByteBufAllocator.java"]
  e26ce83f_6a02_6e37_2f80_6f284af38963 -->|defined in| 6b49ef9c_b171_f31a_8052_bb3056b414c3
  60e2f59e_7d66_d3e5_d382_769ecbb8ab1a["AdaptiveByteBufAllocator()"]
  e26ce83f_6a02_6e37_2f80_6f284af38963 -->|method| 60e2f59e_7d66_d3e5_d382_769ecbb8ab1a
  67436e07_77af_f59b_20fa_26caf7004b15["ByteBuf()"]
  e26ce83f_6a02_6e37_2f80_6f284af38963 -->|method| 67436e07_77af_f59b_20fa_26caf7004b15
  9811fd4e_aa8c_848d_538c_af53636c647d["isDirectBufferPooled()"]
  e26ce83f_6a02_6e37_2f80_6f284af38963 -->|method| 9811fd4e_aa8c_848d_538c_af53636c647d
  b1434579_a6e2_2263_61a2_98f38aaa8b64["usedHeapMemory()"]
  e26ce83f_6a02_6e37_2f80_6f284af38963 -->|method| b1434579_a6e2_2263_61a2_98f38aaa8b64
  18d985d2_e8b2_6a29_b841_ac9f3901bf48["usedDirectMemory()"]
  e26ce83f_6a02_6e37_2f80_6f284af38963 -->|method| 18d985d2_e8b2_6a29_b841_ac9f3901bf48
  4ca89cdb_a75f_a025_bdbb_946bf121b3aa["ByteBufAllocatorMetric()"]
  e26ce83f_6a02_6e37_2f80_6f284af38963 -->|method| 4ca89cdb_a75f_a025_bdbb_946bf121b3aa

Relationship Graph

Source Code

buffer/src/main/java/io/netty/buffer/AdaptiveByteBufAllocator.java lines 31–119

public final class AdaptiveByteBufAllocator extends AbstractByteBufAllocator
        implements ByteBufAllocatorMetricProvider, ByteBufAllocatorMetric {
    private static final InternalLogger logger = InternalLoggerFactory.getInstance(AdaptiveByteBufAllocator.class);
    private static final boolean DEFAULT_USE_CACHED_MAGAZINES_FOR_NON_EVENT_LOOP_THREADS;

    static {
        DEFAULT_USE_CACHED_MAGAZINES_FOR_NON_EVENT_LOOP_THREADS = SystemPropertyUtil.getBoolean(
                "io.netty.allocator.useCachedMagazinesForNonEventLoopThreads", false);
        logger.debug("-Dio.netty.allocator.useCachedMagazinesForNonEventLoopThreads: {}",
                     DEFAULT_USE_CACHED_MAGAZINES_FOR_NON_EVENT_LOOP_THREADS);
    }

    private final AdaptivePoolingAllocator direct;
    private final AdaptivePoolingAllocator heap;

    public AdaptiveByteBufAllocator() {
        this(!PlatformDependent.isExplicitNoPreferDirect());
    }

    public AdaptiveByteBufAllocator(boolean preferDirect) {
        this(preferDirect, DEFAULT_USE_CACHED_MAGAZINES_FOR_NON_EVENT_LOOP_THREADS);
    }

    public AdaptiveByteBufAllocator(boolean preferDirect, boolean useCacheForNonEventLoopThreads) {
        super(preferDirect);
        direct = new AdaptivePoolingAllocator(new DirectChunkAllocator(this), useCacheForNonEventLoopThreads);
        heap = new AdaptivePoolingAllocator(new HeapChunkAllocator(this), useCacheForNonEventLoopThreads);
    }

    @Override
    protected ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity) {
        return toLeakAwareBuffer(heap.allocate(initialCapacity, maxCapacity));
    }

    @Override
    protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) {
        return toLeakAwareBuffer(direct.allocate(initialCapacity, maxCapacity));
    }

    @Override
    public boolean isDirectBufferPooled() {
        return true;
    }

    @Override
    public long usedHeapMemory() {
        return heap.usedMemory();
    }

    @Override
    public long usedDirectMemory() {
        return direct.usedMemory();
    }

    @Override
    public ByteBufAllocatorMetric metric() {
        return this;
    }

    private static final class HeapChunkAllocator implements AdaptivePoolingAllocator.ChunkAllocator {
        private final ByteBufAllocator allocator;

        private HeapChunkAllocator(ByteBufAllocator allocator) {
            this.allocator = allocator;
        }

        @Override
        public AbstractByteBuf allocate(int initialCapacity, int maxCapacity) {
            return PlatformDependent.hasUnsafe() ?
                    new UnpooledUnsafeHeapByteBuf(allocator, initialCapacity, maxCapacity) :
                    new UnpooledHeapByteBuf(allocator, initialCapacity, maxCapacity);
        }
    }

    private static final class DirectChunkAllocator implements AdaptivePoolingAllocator.ChunkAllocator {
        private final ByteBufAllocator allocator;

        private DirectChunkAllocator(ByteBufAllocator allocator) {
            this.allocator = allocator;
        }

Frequently Asked Questions

What is the AdaptiveByteBufAllocator class?
AdaptiveByteBufAllocator is a class in the netty codebase, defined in buffer/src/main/java/io/netty/buffer/AdaptiveByteBufAllocator.java.
Where is AdaptiveByteBufAllocator defined?
AdaptiveByteBufAllocator is defined in buffer/src/main/java/io/netty/buffer/AdaptiveByteBufAllocator.java at line 31.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free