在属性上使用删除时的 Javascript 对象内存管理
我目前正在编写一个 node.js/socket.io 应用程序,但问题对于 javascript 来说是普遍的。
我有一个关联数组,用于存储每个客户端连接的颜色。考虑以下事项:
var clientColors = new Array();
//This execute each new connection
socket.on('connection', function(client){
clientColors[client.sessionId] = "red";
//This execute each time a client disconnect
client.on('disconnect', function () {
delete clientColors[client.sessionId];
});
});
如果我使用delete语句,我担心它会造成内存泄漏,因为以client.sessionId
值命名的属性(关联数组是对象)不会被删除,它的引用它的值将消失,但该属性仍将存在于对象中。
我说得对吗?
I'm currently writing a node.js/socket.io application but the question is general to javascript.
I have an associative array that store a color for each client connection. Consider the following:
var clientColors = new Array();
//This execute each new connection
socket.on('connection', function(client){
clientColors[client.sessionId] = "red";
//This execute each time a client disconnect
client.on('disconnect', function () {
delete clientColors[client.sessionId];
});
});
If I use the delete statement, I fear that it will make a memory leak as the property named after client.sessionId
value(associative arrays are objects) won't be deleted, its reference to its value will be gonne but the property will still exist in the object.
Am I right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
delete clientColors[client.sessionId];
这将删除对对象
clientColors
上的对象的引用。 v8 垃圾收集器将拾取任何具有零引用的对象进行垃圾收集。现在,如果您问这是否会在 IE4 中造成内存泄漏,那么那就是另一个问题了。另一方面,v8 可以轻松处理这个问题。
当您删除唯一的引用时,该属性也将消失。另请注意,对象不是 javascript 中的“关联数组”,因为排序是特定于实现的,并且不受 ES 规范的保证。
delete clientColors[client.sessionId];
This will remove the reference to the object on object
clientColors
. The v8 garbage collector will pick up any objects with zero references for garbage collection.Now if you asked whether this created a memory leak in IE4 then that's a different question. v8 on the other hand can handle this easily.
Seeing as you deleted the only reference the property will also be gone. Also note that objects are not "associative arrays" in javascript since ordering is implementation specific and not garantueed by the ES specification.
由于在这种情况下
clientColors[client.sessionId]
是一个原始值(字符串),因此它将立即被清除。让我们考虑一下更复杂的情况:
foo[bar] = o
,其中o
是非基元(某个对象)。重要的是要理解o
并不是存储在foo
的“内部”,而是存储在任意内存位置的某个位置。foo
仅保存指向该位置的指针。当您调用delete foo[bar]
时,该指针将被清除,并且由垃圾收集器来释放o
占用的内存。顺便说一句:当您需要关联数组时,不应该使用
Array
。后者在 Javascript 中称为Object
,通常使用简写准文字{}
进行实例化Since
clientColors[client.sessionId]
is a primitive value (a string) in this case, it will be cleared immediately.Let's consider the more complicated case of
foo[bar] = o
witho
being a non-primitive (some object). It's important to understand thato
is not stored "inside"foo
, but somewhere in an arbitrary memory location.foo
merely holds a pointer to that location. When you calldelete foo[bar]
, that pointer is cleared, and it's up to the garbage collector to free the memory taken byo
.BTW: You shouldn't use
Array
when you want an associative array. The latter is calledObject
in Javascript and is usually instantiated using the short-hand quasi-literal{}
如果您使用V8引擎或nodejs/io,它可能不会导致泄漏,但始终建议防止泄漏。
只需删除它
或将其设置为 null
这也会级联到任何原型继承的对象。
这样几乎就不可能出现泄漏。
If you are using the V8 engine or nodejs/io, it may not lead to a leak but it is always advisable to prevent leaks.
Just delete it
Or set it to null
Which will also cascade to any prototypically inherited objects.
This way there is almost no probability of starting a leak.