简体   繁体   English

在属性上使用Delete时的Javascript对象内存管理

[英]Javascript object memory management when using delete on property

I'm currently writing a node.js/socket.io application but the question is general to javascript. 我目前正在编写一个node.js / socket.io应用程序,但问题是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. 如果我使用DELETE语句,我担心它会让内存泄漏作为命名的属性client.sessionId值(关联数组对象)将不会被删除,其引用到它的价值将是内[114]但该属性仍存在于对象中。

Am I right? 我对吗?

delete clientColors[client.sessionId];

This will remove the reference to the object on object clientColors . 这将删除对对象clientColors上对象的引用。 The v8 garbage collector will pick up any objects with zero references for garbage collection. v8垃圾收集器将选择引用的所有对象进行垃圾收集。

Now if you asked whether this created a memory leak in IE4 then that's a different question. 现在,如果您问这是否在IE4中造成了内存泄漏,那就是另一个问题。 v8 on the other hand can handle this easily. 另一方面,v8可以轻松解决此问题。

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. 还要注意,对象不是javascript中的“关联数组”,因为排序是实现特定的,并且不受ES规范的约束。

Since clientColors[client.sessionId] is a primitive value (a string) in this case, it will be cleared immediately. 由于在这种情况下clientColors[client.sessionId]是原始值(字符串),因此将立即清除它。

Let's consider the more complicated case of foo[bar] = o with o being a non-primitive (some object). 让我们考虑foo[bar] = o的更复杂的情况,其中o是非原始的(某些对象)。 It's important to understand that o is not stored "inside" foo , but somewhere in an arbitrary memory location. 重要的是要了解o不是存储在foo内部,而是存储在任意内存位置中。 foo merely holds a pointer to that location. foo仅持有指向该位置的指针。 When you call delete foo[bar] , that pointer is cleared, and it's up to the garbage collector to free the memory taken by o . 当您调用delete foo[bar] ,该指针将被清除,由垃圾回收器释放o占用的内存。

BTW: You shouldn't use Array when you want an associative array. BTW:你不应该使用Array当你想要一个关联数组。 The latter is called Object in Javascript and is usually instantiated using the short-hand quasi-literal {} 后者在Javascript中称为Object ,通常使用简写的准文字{}实例化

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. 如果您使用的是V8引擎或nodejs / io,则可能不会导致泄漏,但始终建议您防止泄漏。

Just delete it 只要删除它

delete clientColors[client.sessionId];

Or set it to null 或将其设置为null

clientColors[client.sessionId] = null;

Which will also cascade to any prototypically inherited objects. 这也将级联到任何原型继承的对象。

This way there is almost no probability of starting a leak. 这样,几乎没有发生泄漏的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM