gpt4 book ai didi

Netty 4.1 : ioBuffer v. 秒。 directBuffer 对比堆缓冲区

转载 作者:行者123 更新时间:2023-12-04 17:58:10 26 4
gpt4 key购买 nike

我正在创建要通过其 writewriteAndFlush 方法发送到 Channel 的 ByteBuf。

据我了解,在这种情况下,直接缓冲区优于堆缓冲区,因为后者由 JVM 管理,需要在发送时复制到直接缓冲区。

直接缓冲区不受 JVM 管理,因此分配大量直接缓冲区可能会导致 OutOfMemoryError正确吗?

我注意到还有另一种方法 ByteBufAllocator.ioBuffer()文件说

Allocate a ByteBuf, preferably a direct buffer which is suitable for I/O.

我猜它是这样工作的:
ByteBufAllocator 管理着两个pool,一个pool为direct buffer,一个为heap buffer。如果直接缓冲区空间不足,则从堆缓冲区分配。 我说得对吗?

ByteBufAllocator如何确定直接缓冲区的池大小?

最佳答案

Direct buffer is not managed by JVM, so allocate a lot of direct buffer may cause OutOfMemoryError, correct?

分配过多的东西而不释放它会导致 OOM。你可能会看到这样的东西:java.lang.OutOfMemoryError: 直接缓冲内存

I guess it works in this way: ByteBufAllocator manages two pools, one pool for direct buffer, the other for heap buffer. If there is insufficient space in direct buffer, then this allocates from heap buffer. Am I right?

真相比你的猜测还要简单。 AbstractByteBufAllocator.ioBuffer只要它能在类路径上找到 sun.misc.Unsafe,它就会为你调用 directBuffer,否则它会回退到使用 heapBuffer

public ByteBuf ioBuffer() {
if (PlatformDependent.hasUnsafe()) {
return directBuffer(DEFAULT_INITIAL_CAPACITY);
}
return heapBuffer(DEFAULT_INITIAL_CAPACITY);
}

关于Netty 4.1 : ioBuffer v. 秒。 directBuffer 对比堆缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38638411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com