Node.js缓冲垃圾收集 [英] Node.js buffer garbage collection

查看:204
本文介绍了Node.js缓冲垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

buf.slice([start [,end]])

返回一个新的Buffer,它引用与原始内存相同的内存,但是在开始时偏移并裁剪请注意,修改新的缓冲区片将修改原始缓冲区中的内存,因为两个对象的分配内存重叠。

$ b


$ b

如果其中一个引用不存在,垃圾回收器如何处理分配的内存?

解决方案

你在缓冲区上执行切片,你只是创建一个新的引用到原始缓冲区,它在不同的点开始和结束。



如果更改原始缓冲区,切片的引用也会改变。

这意味着整个内存块将无法用于垃圾收集,直到所有引用(切片或不切片)都消失。



希望这能回答你的问题。


buf.slice([start[, end]])

Returns a new Buffer that references the same memory as the original, but offset and cropped by the start and end indices.

Note that modifying the new Buffer slice will modify the memory in the original Buffer because the allocated memory of the two objects overlap.

How does garbage collector handle allocated memory if one of the references is gone?

解决方案

When you perform a slice on a Buffer, you are only creating a new reference to the original buffer, which starts and ends at different points.

If you change the original buffer, the sliced reference will also change.

What this means is that the entire chunk of memory won't be available for garbage collection until all references (sliced or not) are gone.

Hope this answers your question.

这篇关于Node.js缓冲垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆