netty 参数整理

buffer

io.netty.buffer.bytebuf.checkAccessible

默认值 `true`

是否对每一次 Bytebuf 操作检查 是否使用了已被 release 的 buf

AbstractByteBuf

io.netty.leakDetection.acquireAndReleaseOnly (false)

疑似是否仅对 acquire 和 release ByteBuf 启用 `ResourceLeakTracker`

AdvancedLeakAwareByteBuf

TODO referenceQueue

io.netty.threadLocalDirectBufferSize (0)

是否启用 threadlocal 的堆外内存, size > 0 则启用, 表示缓存个数, 当不启用 PooledBytebuf的时候才有效。

ByteBufUtil

io.netty.maxThreadLocalCharBufferSize (16384)

当 将bytebuf 转 string 时, 当申请大小 小于这个阈值的, 将会被 ThreadLocal 缓存。

ByteBufUtil

io.netty.allocator.pageSize (8k)

当使用 PooledByteBuf 时, 小于这个页的大小, 并且 大于512 的被认为是 small page,
大于这个大小 且小于 chunksize 的被认为是 Normal page

PooledByteBufAllocator

io.netty.allocator.maxOrder (11)

pagesize 左移 11位 即为chunk size

PooledByteBufAllocator

io.netty.allocator.tinyCacheSize (512)

每个线程中(PoolThreadCache) 缓存 tinycache 的个数, 超过的不会存入queue

PooledByteBufAllocator

io.netty.allocator.smallCacheSize (256)

每个线程中(PoolThreadCache) 缓存 smallCache 的个数, 超过的不会存入queue

PooledByteBufAllocator

io.netty.allocator.normalCacheSize (64)

每个线程中(PoolThreadCache) 缓存 normalCache 的个数, 超过的不会存入queue

PooledByteBufAllocator

io.netty.allocator.maxCachedBufferCapacity (32768)

每个线程中(PoolThreadCache) 缓存的 normalCache 的最大大小。

PooledByteBufAllocator

io.netty.allocator.cacheTrimInterval (8192)

每个线程中(PoolThreadCache) 经过多久次 get 操作后, 全部清理一次

PooledByteBufAllocator

io.netty.allocator.useCacheForAllThreads (true)

是否每一个线程都适用(PoolThreadCache)

PooledByteBufAllocator

io.netty.allocator.directMemoryCacheAlignment (0)

对外内存申请, 是否保证堆外内存对齐, > 0 才启用。
directMemoryCacheAlignmentMask 为 directMemoryCacheAlignment - 1

PooledByteBufAllocator

io.netty.noJdkZlibDecoder(jdk<7) 和 io.netty.noJdkZlibEncoder (false)

启用 jdk 自己的 zlib decoder 和 encoder

ZlibCodecFactory

io.netty.http2.childrenMapSize (22)

WeightedFairQueueByteDistributor

io.netty.defaultPromise.maxListenerStackDepth (8)

控制 addlistener 的最大嵌套层数, 超过这个层数的 listener 不会跑错, 而是不会被执行

DefaultPromise

io.netty.eventexecutor.maxPendingTasks (Integer.MAX_VALUE)

netty 最大的异步任务数量, 比如 writeAndFlush 就是一个异步任务。

比如同时存在 大量的 writeAndFlush 任务则会 积压这个queue

SingleThreadEventExecutor

io.netty.threadLocalMap.stringBuilder.initialSize (1024)

ThreadLocal 的 StringBuilder 的初始申请空间大小

InternalThreadLocalMap

io.netty.threadLocalMap.stringBuilder.maxSize (4096)

ThreadLocal 的 StringBuilder 的申请空间大小的最大阈值

InternalThreadLocalMap

io.netty.util.internal.ObjectCleaner.refQueuePollTimeout (10000)

用于检测内存泄漏的 referenceQueue 的最大pool timeout

ObjectCleaner

io.netty.native.deleteLibAfterLoading io.netty.native.workdir io.netty.native.tryPatchShadedId

仅当使用了 类似 EpollEventLoop 时 使用

NativeLibraryLoader

io.netty.noPreferDirect (false)

是否优先使用 堆外内存

PlatformDependent

io.netty.maxDirectMemory

最大堆外内存策略

// Here is how the system property is used:
//
// < 0 - Don’t use cleaner, and inherit max direct memory from java. In this case the
// “practical max direct memory” would be 2
max memory as defined by the JDK.
// == 0 - Use cleaner, Netty will not enforce max memory, and instead will defer to JDK.
//
> 0 - Don’t use cleaner. This will limit Netty’s total direct memory
// (note: that JDK’s direct memory limit is independent of this).

PlatformDependent

io.netty.uninitializedArrayAllocationThreshold (-1)

初始化数组的最大阈值, 满足不了的将用 反射了的数组替换

PlatformDependent

io.netty.noUnsafe (false)

不使用 Unsafe

PlatformDependent0

io.netty.initialSeedUniquifier (0) java.util.secureRandomSeed (false)

是否使用 jdk的 secureRandom 生成随即种子(需要想native /dev/random 获取随机数据,较慢)

默认不启用

ThreadLocalRandom

io.netty.availableProcessors (Runtime.getRuntime().availableProcessors())

系统可用的核数, 默认为系统提供的核数。

NettyRuntime

java.net.preferIPv4Stack (false) java.net.preferIPv6Addresses (false)

当系统多系统时, 优先使用 ipv4 / ipv6

NetUtil

io.netty.net.somaxconn.trySysctl (false)

如果系统的安全设置比较严格, 仅可以通过 systemctl 方式修改 somaxconn (内核指定的最大连接数)

NetUtil

io.netty.recycler.maxCapacityPerThread (io.netty.recycler.maxCapacity (4096))

使用 内部的 threadlocal 的 objectPool时, 每个线程的最大threadlocal 的容量。

Recycler

io.netty.recycler.maxSharedCapacityFactor (2)

多线程之间共享 objectPool 数量因子 (TODO)

Recycler

https://github.com/netty/netty/blob/14158070bf8b3f831702061ffd657c84179fc639/common/src/main/java/io/netty/util/Recycler.java

io.netty.recycler.maxDelayedQueuesPerThread (NettyRuntime.availableProcessors() * 2)

最大延迟queue 数量

Recycler 跨线程的最多数量

io.netty.recycler.linkCapacity (16)

WeakOrderQueue 中线程可以缓存的对象数量

Recycler

io.netty.recycler.ratio (8)

根据一定有概率的丢弃 缓存对象, 1/8的概率

Recycler

io.netty.customResourceLeakDetector (“”)

自定义的 泄漏检测模块

ResourceLeakDetectorFactory

io.netty.noResourceLeakDetection (null)

无资源检测

ResourceLeakDetector

io.netty.serviceThreadPrefix (“”)

用于检测线程是否可用的 一个 watcher, 线程从 里面 建立线程的命名配置

ThreadDeathWatcher

io.netty.transport.estimateSizeOnSubmit (true)

是否启用对每一次 写出包,统计她们的大小, 作为 WriteBufferHighWaterMark 的基础数据。

AbstractChannelHandlerContext

io.netty.transport.writeTaskSizeOverhead (48)

内存中统计 积压的 写task 占用的内存, 默认的计算方式是基于

Assuming a 64-bit JVM, 16 bytes object header, 3 reference fields and one int field, plus alignment

计算的。

非 64位的 JVM 则会统计失常。

AbstractChannelHandlerContext

io.netty.transport.outboundBufferEntrySizeOverhead (96)

每一个 写出的消息关联的 Entry 对象占用的 内存pending, 同上,基于64位 系统计算

// Assuming a 64-bit JVM:
// - 16 bytes object header
// - 8 reference fields
// - 2 long fields
// - 2 int fields
// - 1 boolean field
// - padding

ChannelOutboundBuffer

io.netty.eventLoopThreads (NettyRuntime.availableProcessors() * 2)

全局默认的 eventLoop线程个数, 当配置 thread 个数位0 时,才取该配置。

MultithreadEventLoopGroup

io.netty.noKeySetOptimization (false)

是否放弃优化 selector 的 keyset, 将 HashSet 优化为 数组

NioEventLoop

io.netty.selectorAutoRebuildThreshold (512)

当发生 jvm 的空select 异常时, 至少空 select 512 轮, 才重新创建 selector 对象

NioEventLoop

io.netty.transport.pendingWriteSizeOverhead (64)

同上pending, 用于 写queue pending 的内存统计的 pending

Assuming a 64-bit JVM:

  • 16 bytes object header
  • 4 reference fields
  • 1 long fields

PendingWriteQueue

io.netty.eventLoop.maxPendingTasks (Integer.MAX_VALUE)

此queue 的添加部分目前还是 @UnstableApi

SingleThreadEventLoop

io.netty.transport.noNative (false)

如果使用 Epoll 的话 不可以为 true

Epoll / KQueue
```

avatar

lelouchcr's blog