Home / Class/ Recycler Class — netty Architecture

Recycler Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  2e480a34_d633_ddbb_a405_05fdc823a6b2["Recycler"]
  92da7dba_a9f6_c802_c3c1_5a54e228e444["Recycler.java"]
  2e480a34_d633_ddbb_a405_05fdc823a6b2 -->|defined in| 92da7dba_a9f6_c802_c3c1_5a54e228e444
  790dd86e_deb1_12c1_5a78_39741f72e055["Recycler()"]
  2e480a34_d633_ddbb_a405_05fdc823a6b2 -->|method| 790dd86e_deb1_12c1_5a78_39741f72e055
  4f93175b_cd34_2721_02f8_8d0965fa4ec6["T()"]
  2e480a34_d633_ddbb_a405_05fdc823a6b2 -->|method| 4f93175b_cd34_2721_02f8_8d0965fa4ec6
  66c41ae5_3da8_27ce_6a7a_65b2c571721d["unpinOwner()"]
  2e480a34_d633_ddbb_a405_05fdc823a6b2 -->|method| 66c41ae5_3da8_27ce_6a7a_65b2c571721d
  96907eec_e3dc_54dd_21b5_d9a6e29acb49["recycle()"]
  2e480a34_d633_ddbb_a405_05fdc823a6b2 -->|method| 96907eec_e3dc_54dd_21b5_d9a6e29acb49
  3dd8d308_b2cc_2e94_8910_2fb848875a0b["threadLocalSize()"]
  2e480a34_d633_ddbb_a405_05fdc823a6b2 -->|method| 3dd8d308_b2cc_2e94_8910_2fb848875a0b

Relationship Graph

Source Code

common/src/main/java/io/netty/util/Recycler.java lines 44–729

public abstract class Recycler<T> {
    private static final InternalLogger logger = InternalLoggerFactory.getInstance(Recycler.class);

    /**
     * We created this handle to avoid having more than 2 concrete implementations of {@link EnhancedHandle}
     * i.e. NOOP_HANDLE, {@link DefaultHandle} and the one used in the LocalPool.
     */
    private static final class LocalPoolHandle<T> extends EnhancedHandle<T> {
        private final UnguardedLocalPool<T> pool;

        private LocalPoolHandle(UnguardedLocalPool<T> pool) {
            this.pool = pool;
        }

        @Override
        public void recycle(T object) {
            UnguardedLocalPool<T> pool = this.pool;
            if (pool != null) {
                pool.release(object);
            }
        }

        @Override
        public void unguardedRecycle(final Object object) {
            UnguardedLocalPool<T> pool = this.pool;
            if (pool != null) {
                pool.release((T) object);
            }
        }
    }

    private static final EnhancedHandle<?> NOOP_HANDLE = new LocalPoolHandle<>(null);
    private static final UnguardedLocalPool<?> NOOP_LOCAL_POOL = new UnguardedLocalPool<>(0);
    private static final int DEFAULT_INITIAL_MAX_CAPACITY_PER_THREAD = 4 * 1024; // Use 4k instances as default.
    private static final int DEFAULT_MAX_CAPACITY_PER_THREAD;
    private static final int RATIO;
    private static final int DEFAULT_QUEUE_CHUNK_SIZE_PER_THREAD;
    private static final boolean BLOCKING_POOL;
    private static final boolean BATCH_FAST_TL_ONLY;

    static {
        // In the future, we might have different maxCapacity for different object types.
        // e.g. io.netty.recycler.maxCapacity.writeTask
        //      io.netty.recycler.maxCapacity.outboundBuffer
        int maxCapacityPerThread = SystemPropertyUtil.getInt("io.netty.recycler.maxCapacityPerThread",
                SystemPropertyUtil.getInt("io.netty.recycler.maxCapacity", DEFAULT_INITIAL_MAX_CAPACITY_PER_THREAD));
        if (maxCapacityPerThread < 0) {
            maxCapacityPerThread = DEFAULT_INITIAL_MAX_CAPACITY_PER_THREAD;
        }

        DEFAULT_MAX_CAPACITY_PER_THREAD = maxCapacityPerThread;
        DEFAULT_QUEUE_CHUNK_SIZE_PER_THREAD = SystemPropertyUtil.getInt("io.netty.recycler.chunkSize", 32);

        // By default, we allow one push to a Recycler for each 8th try on handles that were never recycled before.
        // This should help to slowly increase the capacity of the recycler while not be too sensitive to allocation
        // bursts.
        RATIO = max(0, SystemPropertyUtil.getInt("io.netty.recycler.ratio", 8));

        BLOCKING_POOL = SystemPropertyUtil.getBoolean("io.netty.recycler.blocking", false);
        BATCH_FAST_TL_ONLY = SystemPropertyUtil.getBoolean("io.netty.recycler.batchFastThreadLocalOnly", true);

        if (logger.isDebugEnabled()) {
            if (DEFAULT_MAX_CAPACITY_PER_THREAD == 0) {
                logger.debug("-Dio.netty.recycler.maxCapacityPerThread: disabled");
                logger.debug("-Dio.netty.recycler.ratio: disabled");
                logger.debug("-Dio.netty.recycler.chunkSize: disabled");
                logger.debug("-Dio.netty.recycler.blocking: disabled");
                logger.debug("-Dio.netty.recycler.batchFastThreadLocalOnly: disabled");
            } else {
                logger.debug("-Dio.netty.recycler.maxCapacityPerThread: {}", DEFAULT_MAX_CAPACITY_PER_THREAD);
                logger.debug("-Dio.netty.recycler.ratio: {}", RATIO);
                logger.debug("-Dio.netty.recycler.chunkSize: {}", DEFAULT_QUEUE_CHUNK_SIZE_PER_THREAD);
                logger.debug("-Dio.netty.recycler.blocking: {}", BLOCKING_POOL);
                logger.debug("-Dio.netty.recycler.batchFastThreadLocalOnly: {}", BATCH_FAST_TL_ONLY);
            }
        }
    }

    private final LocalPool<?, T> localPool;
    private final FastThreadLocal<LocalPool<?, T>> threadLocalPool;

Frequently Asked Questions

What is the Recycler class?
Recycler is a class in the netty codebase, defined in common/src/main/java/io/netty/util/Recycler.java.
Where is Recycler defined?
Recycler is defined in common/src/main/java/io/netty/util/Recycler.java at line 44.

Analyze Your Own Codebase

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

Try Supermodel Free