Node.js 中的“非池化”缓冲区是什么?

问题描述 投票:0回答:1

我在阅读 Node.js Buffers 的文档时遇到了这个术语,我注意到他们写的是“un-pooled”,而不是 Netty 中的“unpooled”,例如,如果概念相同,请尝试解释一下与处理类似问题时采用的方式不同。我的第二个问题,如果存在“非池化”缓冲区,是否存在“池化”缓冲区,它们是什么?

javascript node.js memory buffer
1个回答
0
投票

非池化缓冲区:

在非池化缓冲区中,每个缓冲区都是独立分配的,内存由开发人员手动管理

// Example of creating a non-pooled buffer
const buffer = Buffer.alloc(10);

池化缓冲区:

池化缓冲区由池或类池机制管理,其中维护预分配缓冲区池。

不是每次都直接从系统分配内存,而是从池中获取池化缓冲区。使用后可以放回池中以供以后再使用。

// Example of creating a pooled buffer
const pool = new BufferPool(10); 
const buffer = pool.allocate(10);
// After use, return the buffer to the pool
pool.release(buffer);
© www.soinside.com 2019 - 2024. All rights reserved.