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

时间:2022-09-25 08:40:24

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值(关联数组为对象)命名的属性,它对其值的引用将是gonne但属性仍然是存在于对象中。

Am I right?

我对吗?

3 个解决方案

#1


12  

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.

这将删除对象clientColors上对象的引用。 v8垃圾收集器将拾取任何没有引用垃圾收集的对象。

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.

现在,如果您询问这是否在IE4中创建了内存泄漏,那么这是一个不同的问题。另一方面,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规范的保证。

#2


4  

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). It's important to understand that o is not stored "inside" foo, but somewhere in an arbitrary memory location. foo merely holds a pointer to that location. 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.

让我们考虑更复杂的foo [bar] = o的情况,其中o是非原始的(某个对象)。重要的是要理解o不是存储在foo内部,而是存储在任意内存位置。 foo只是拥有一个指向该位置的指针。当你调用delete foo [bar]时,该指针被清除,并由垃圾收集器来释放o占用的内存。

BTW: You shouldn't use Array when you want an associative array. The latter is called Object in Javascript and is usually instantiated using the short-hand quasi-literal {}

顺便说一句:当你想要一个关联数组时,你不应该使用Array。后者在Javascript中称为Object,通常使用short-hand quasi-literal {}进行实例化

#3


-1  

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.

这样几乎不可能发生泄漏。

#1


12  

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.

这将删除对象clientColors上对象的引用。 v8垃圾收集器将拾取任何没有引用垃圾收集的对象。

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.

现在,如果您询问这是否在IE4中创建了内存泄漏,那么这是一个不同的问题。另一方面,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规范的保证。

#2


4  

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). It's important to understand that o is not stored "inside" foo, but somewhere in an arbitrary memory location. foo merely holds a pointer to that location. 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.

让我们考虑更复杂的foo [bar] = o的情况,其中o是非原始的(某个对象)。重要的是要理解o不是存储在foo内部,而是存储在任意内存位置。 foo只是拥有一个指向该位置的指针。当你调用delete foo [bar]时,该指针被清除,并由垃圾收集器来释放o占用的内存。

BTW: You shouldn't use Array when you want an associative array. The latter is called Object in Javascript and is usually instantiated using the short-hand quasi-literal {}

顺便说一句:当你想要一个关联数组时,你不应该使用Array。后者在Javascript中称为Object,通常使用short-hand quasi-literal {}进行实例化

#3


-1  

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.

这样几乎不可能发生泄漏。