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

发布于 2024-11-02 22:07:54 字数 561 浏览 1 评论 0原文

我目前正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄荷→糖丶微凉 2024-11-09 22:07:54

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.

你的笑 2024-11-09 22:07:54

由于在这种情况下 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 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.

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 {}

巷雨优美回忆 2024-11-09 22:07:54

如果您使用V8引擎或nodejs/io,它可能不会导致泄漏,但始终建议防止泄漏。

只需删除它

delete clientColors[client.sessionId];

或将其设置为 null

clientColors[client.sessionId] = 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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文