Example usage for io.netty.buffer PooledByteBufAllocator PooledByteBufAllocator

List of usage examples for io.netty.buffer PooledByteBufAllocator PooledByteBufAllocator

Introduction

In this page you can find the example usage for io.netty.buffer PooledByteBufAllocator PooledByteBufAllocator.

Prototype

public PooledByteBufAllocator(boolean preferDirect, int nHeapArena, int nDirectArena, int pageSize,
            int maxOrder, int tinyCacheSize, int smallCacheSize, int normalCacheSize,
            boolean useCacheForAllThreads) 

Source Link

Usage

From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorImpl.java

License:Apache License

ByteBufAllocatorImpl(ByteBufAllocator pooledAllocator, ByteBufAllocator unpooledAllocator,
        PoolingPolicy poolingPolicy, int poolingConcurrency, OutOfMemoryPolicy outOfMemoryPolicy,
        Consumer<OutOfMemoryError> outOfMemoryListener, LeakDetectionPolicy leakDetectionPolicy) {
    super(poolingPolicy == PoolingPolicy.PooledDirect /* preferDirect */);

    this.poolingPolicy = poolingPolicy;
    this.outOfMemoryPolicy = outOfMemoryPolicy;
    if (outOfMemoryListener == null) {
        this.outOfMemoryListener = (v) -> {
            log.error("Unable to allocate memory", v);
        };// w w  w . jav  a  2 s  .c o  m
    } else {
        this.outOfMemoryListener = outOfMemoryListener;
    }

    if (poolingPolicy == PoolingPolicy.PooledDirect) {
        if (pooledAllocator == null) {
            if (poolingConcurrency == PooledByteBufAllocator.defaultNumDirectArena()) {
                // If all the parameters are the same as in the default Netty pool,
                // just reuse the static instance as the underlying allocator.
                this.pooledAllocator = PooledByteBufAllocator.DEFAULT;
            } else {
                this.pooledAllocator = new PooledByteBufAllocator(true /* preferDirect */,
                        poolingConcurrency /* nHeapArena */, poolingConcurrency /* nDirectArena */,
                        PooledByteBufAllocator.defaultPageSize(), PooledByteBufAllocator.defaultMaxOrder(),
                        PooledByteBufAllocator.defaultTinyCacheSize(),
                        PooledByteBufAllocator.defaultSmallCacheSize(),
                        PooledByteBufAllocator.defaultNormalCacheSize(),
                        PooledByteBufAllocator.defaultUseCacheForAllThreads());
            }
        } else {
            this.pooledAllocator = pooledAllocator;
        }
    } else {
        this.pooledAllocator = null;
    }

    this.unpooledAllocator = (unpooledAllocator != null) ? unpooledAllocator : UnpooledByteBufAllocator.DEFAULT;

    // The setting is static in Netty, so it will actually affect all
    // allocators
    switch (leakDetectionPolicy) {
    case Disabled:
        if (log.isDebugEnabled()) {
            log.debug("Disable Netty allocator leak detector");
        }
        ResourceLeakDetector.setLevel(Level.DISABLED);
        break;

    case Simple:
        log.info("Setting Netty allocator leak detector to Simple");
        ResourceLeakDetector.setLevel(Level.SIMPLE);
        break;

    case Advanced:
        log.info("Setting Netty allocator leak detector to Advanced");
        ResourceLeakDetector.setLevel(Level.ADVANCED);
        break;

    case Paranoid:
        log.info("Setting Netty allocator leak detector to Paranoid");
        ResourceLeakDetector.setLevel(Level.PARANOID);
        break;
    }
}

From source file:org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase.java

License:Apache License

@Parameterized.Parameters(name = "{0}")
public static Object[][] dataBufferFactories() {
    return new Object[][] { { new NettyDataBufferFactory(new UnpooledByteBufAllocator(true)) },
            { new NettyDataBufferFactory(new UnpooledByteBufAllocator(false)) },
            // disable caching for reliable leak detection, see https://github.com/netty/netty/issues/5275
            { new NettyDataBufferFactory(new PooledByteBufAllocator(true, 1, 1, 8192, 11, 0, 0, 0, true)) },
            { new NettyDataBufferFactory(new PooledByteBufAllocator(false, 1, 1, 8192, 11, 0, 0, 0, true)) },
            { new DefaultDataBufferFactory(true) }, { new DefaultDataBufferFactory(false) }

    };//from  w  w w .  j a  va 2  s.  c o  m
}

From source file:org.springframework.core.io.buffer.AbstractDataBufferAllocatingTests.java

License:Apache License

public static Stream<Arguments> dataBufferFactories() {
    return Stream.of(
            arguments("NettyDataBufferFactory - UnpooledByteBufAllocator - preferDirect = true",
                    new NettyDataBufferFactory(new UnpooledByteBufAllocator(true))),
            arguments("NettyDataBufferFactory - UnpooledByteBufAllocator - preferDirect = false",
                    new NettyDataBufferFactory(new UnpooledByteBufAllocator(false))),
            // disable caching for reliable leak detection, see https://github.com/netty/netty/issues/5275
            arguments("NettyDataBufferFactory - PooledByteBufAllocator - preferDirect = true",
                    new NettyDataBufferFactory(new PooledByteBufAllocator(true, 1, 1, 4096, 2, 0, 0, 0, true))),
            arguments("NettyDataBufferFactory - PooledByteBufAllocator - preferDirect = false",
                    new NettyDataBufferFactory(
                            new PooledByteBufAllocator(false, 1, 1, 4096, 2, 0, 0, 0, true))),
            arguments("DefaultDataBufferFactory - preferDirect = true", new DefaultDataBufferFactory(true)),
            arguments("DefaultDataBufferFactory - preferDirect = false", new DefaultDataBufferFactory(false)));
}