对属性使用删除时的 Javascript 对象内存管理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5749237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:11:54  来源:igfitidea点击:

Javascript object memory management when using delete on property

javascriptnode.jssocket.io

提问by Moop

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.sessionIdvalue(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值命名的属性(关联数组是对象)不会被删除,它对其值的引用将被删除,但该属性仍将存在于目的。

Am I right?

我对吗?

回答by Raynos

delete clientColors[client.sessionId];

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 zeroreferences for garbage collection.

这将删除对 object 上的对象的引用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 规范所保证的。

回答by user123444555621

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] = owith obeing a non-primitive (some object). It's important to understand that ois not stored "inside" foo, but somewhere in an arbitrary memory location. foomerely 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] = oo作为一个非基本(一些对象)。重要的是要了解它o不是存储在“内部” foo,而是存储在任意内存位置的某个地方。foo仅持有指向该位置的指针。当您调用 时delete foo[bar],该指针被清除,垃圾收集器负责释放 所占用的内存o

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

顺便说一句:Array当你想要一个关联数组时,你不应该使用。后者Object在 Javascript 中被调用,通常使用简写的 quasi-literal 实例化{}

回答by Ian Mbae

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

或将其设置为空

clientColors[client.sessionId] = null;

Which will also cascade to any prototypically inherited objects.

这也将级联到任何原型继承的对象。

This way there is almost no probability of starting a leak.

这样,几乎没有开始泄漏的可能性。