内存泄漏使用socket.io [英] Memory leaks using socket.io

查看:313
本文介绍了内存泄漏使用socket.io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在手动断开连接时套接字io服务器端没有完全销毁套接字。我发现 github上的这个主题很有用。虽然我正在寻找一些阻止GC清理套接字的变量链接,但我在这里问一个问题。

I've found that sockets are not fully destroyed in socket io server side when manually disconnecting them. I've found this topic on github useful. While I'm looking for some variable-links that prevent GC from cleaning sockets, I'm asking a question here.

如果有人遇到同样的问题,这会非常有帮助。

If anyone here encountered the same problem, this would be much help.

不起作用的代码:

socket.on('disconnect', function(){
    socket.removeAllListeners();
});

///...................

socket.disconnect();

但是,使用受限制的库字段的解决方法:

Workaround that, however, uses restricted library fields:

delete io.sockets[url];
io.j = [];


推荐答案

实际上,当你断开连接时,这是按预期工作的一个套接字,你只是声明你不希望现在从该套接字接收任何更多的数据,实际上破坏套接字你基本上做删除套接字动作。在断开连接事件上使用它,即:

actually, this is working as intended, when you disconnect a socket you simply state you're not expecting to receive any more data from that socket right now, to actually destroy the socket you basically do the delete socket action. Use this on the disconnect event, ie:

socket.on('disconnect', function(){
    delete socket; 
})

你也可以在io.sockets.sockets对象上执行此操作在外部函数上:

you can also do this on the io.sockets.sockets Object on an external function:

function deleteSocket(socketID){
    delete io.sockets.sockets[socketID];
}

这篇关于内存泄漏使用socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆