|
返回一个不被池管理的 Buffer。
大量独立分配的 Buffer 容易带来垃圾,为了避免这个情况,小于 4KB 的空间都是切割自一个较大的独立对象。这种策略既提高了性能也改善了内存使用率。V8 不需要跟踪和清理过多的 Persistent 对象。
当开发者需要将池中一小块数据保留一段时间,比较好的办法是用 SlowBuffer 创建一个不被池管理的 Buffer 实例,并将相应数据拷贝出来。
- // need to keep around a few small chunks of memory
- var store = [];
- socket。on('readable', function() {
- var data = socket。read();
- // allocate for retained data
- var sb = new SlowBuffer(10);
- // copy the data into the new allocation
- data。copy(sb, 0, 0, 10);
- store。push(sb);
- });
复制代码
|
|