- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个使用 socket.io 的基本实时服务器。我的问题涉及闭包和垃圾收集,以及我是否应该费心将套接字连接存储在关联数组中,还是将其留给闭包来管理连接。
我的一个问题是,如果套接字连接断开,如果在断开连接的套接字上调用 emit,同一个套接字连接是否会尝试发送消息?换句话说,当您在断开连接的套接字上调用 socket.emit() 时会发生什么?
下面是一些代码,问题在底部:
var socketio = require('socket.io');
var io = socketio.listen(server);
var EE = require('events').EventEmitter;
var ee = new EE(); //this is actually initialized elsewhere but now you know what it is
var connectedUsers = {}; //associative array to store socket connections by socket.id
io.on('connection', function (socket) {
connectedUsers[socket.id] = socket; //should I bother storing the socket connections in the associative array or just leave it to the closure to store them
socket.on('disconnect', function () {
connectedUsers[socket.id] = null; //should I bother removing
});
ee.on('update',function(data){
socket.emit('update',JSON.stringify(data));
});
ee.on('insert',function(data){
socket.emit('insert',JSON.stringify(data));
});
ee.on('delete',function(data){
socket.emit('delete',JSON.stringify(data));
});
});
}
所以我的主要问题/担忧是,用于闭包的内存量将随着套接字连接数线性增长,并且永远不会停止或减少。 socket.io 断开连接事件是否通过垃圾回收释放内存?
看来我需要重新组织上面的代码来完成两件事:
to allow for garbage collection so that the server memoryfootprint doesn't grow unboundedly
to avoid publishing all the data foreach and every socket connection, which is what it is doing now.
这准确吗?
最佳答案
One question I have is if a socket connection disconnects, will the same socket connection attempt to send a message if emit is invoked on the disconnected socket? In other words, what happens when you call socket.emit() on a disconnected socket?
没有。在断开连接的套接字上对 .emit()
的任何调用都将被忽略。 Here's the relevant source code snippet .
connectedUsers[socket.id] = socket;
//should I bother storing the socket connections in the
// associative array or just leave it to the closure to store them
你不应该存储它们。把它留给关闭。然后以后就不需要将它们设置为空了。
So my primary question/concern is that the amount of memory used for the closures will grow linearly with the number of socket connections and will never stop or decrease. Do socket.io disconnect events release memory via garbage collection?
是的,当套接字断开连接时,东西将被释放用于垃圾回收。 socket.io 将从其内部队列中删除它们,不再有任何引用它们,并且它们将有资格进行垃圾收集。但是,您上面的代码对于发送事件的方式不正确。详情请见下文。
ee.on('update',function(data){
socket.emit('update',JSON.stringify(data));
});
不要这样做。问题是您永远不会调用 removeListener
,因此在每个“更新”事件中,您将为自进程启动以来曾经连接过的每个套接字执行这些回调之一,甚至是断开连接的套接字。那是你的泄漏。相反,对于这种模式,只需在闭包之外设置一次广播:
ee.on('update', function (data) {
io.sockets.emit('update', data);
});
(另外:JSON.stringify 几乎肯定是不必要的。只有当你真的知道你必须在另一边将它作为字符串获取时才这样做。只发出一个对象可能是正确的)
现在对于需要发送到特定套接字而不是所有连接的套接字的情况,您应该使用命名函数,这样您就可以将每个 ee.on
与相应的 配对ee.removeListener
以正确防止幻听。
io.on('connection', function(socket) {
// every socket gets a closure
// and a distinct onUpdate function object
function onUpdate(data) {
socket.emit('update', data);
}
ee.on('update', onUpdate);
socket.on('disconnect', function() {
// removes the handler for this specific socket,
// leaving the others intact
ee.removeListener('update', onUpdate);
});
});
关于node.js - Socket.io 断开事件和相关关闭的垃圾回收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31689098/
我正在构建一个我想要的 FragmentPagerAdapter(带有小图标的那个); 到屏幕中间(水平)的第一个项目 滑动它并使它们向左/向右移动屏幕的五分之一 实现此目标的最佳方法是什么? 这是一
我正在使用 GalleryView 和大约 40 张图片,而且速度很慢,因为没有回收... 任何人都可以向我展示在 getView 方法上对 GalleryView 的基本回收。 public cla
我收到警告 This FragmentManager should be recycled after use with #recycle() 我正在尝试修复它。有什么建议吗? date.setOnC
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 a specific programming problem, a software a
是否可以在 global.asax 中捕获回收事件? 我知道 Application_End 会被触发,但有没有办法知道它是由应用程序池的回收触发的? 谢谢,Lieven Cardoen 又名 Joh
当我第一次启动应用程序时,我以正确的方式获得了抽屉导航。当我向下滚动抽屉时,问题就出现了。元素开始消失,其中一些甚至交换了位置。我试图找出问题所在,但找不到。希望你能帮助我。 我已经使用 getIte
我正在尝试构建一个 Android 应用程序,在其中使用 RecyclerView。 在 RecyclerView 的项目中,我使用一个 TextView 和四个 CheckBox 来选择任何人。 现
回收 IIS7 应用程序池是否会终止任何当前正在执行的请求?还是等待所有请求完成(如排水管停止)? 我不希望回收规则导致我的 WCF 站点出现间歇性错误。 谢谢 最佳答案 不。 By default,
我动态创建 div,并希望通过检查 div 的 ID 以正确的顺序放置新的 div。 创建新数据时,我对数组进行排序并创建一个新的 div 容器。第一次构建 DOM 时,它工作正常,因为我先创建数据,
IIS 中 Web 应用程序的 .net Recycle 的 Java 等价物是什么。 这是在 IIS 之外的 Linux 机器上使用 Java 时的情况。 只是停止和启动应用程序吗? 最佳答案 并非
问题: 假设您的传输速度高达 10 MB/s,那么回收 DatagramPacket 对象(而不是每次发送数据包时创建一个新对象)是否是一个好主意? 故事: 我正在创建一个 LAN 文件同步应用程序,
我已经使用适配器创建了一个 ListView,但唯一的问题是我们使用了不同的布局,因为它是一个消息传递应用程序。我想通过使用 View 回收来提高性能,但不知道如何回收 View 。 是否可以更改 V
我是 Android 初学者,我不明白为什么会这样。 Activity 截图: 一切正常,除非我向下滚动(因此我认为它与回收有关)...所以当我向上滚动并尝试撤消第一篇文章中的投票(红色箭头)时,它认
我在 RecyclerView 中使用 FlexboxLayoutManager,我需要阻止 RV 回收。我尝试将 itemViewCacheSize() 设置为项目总数,但没有帮助。我也尝试将 ma
在我使用 SearchView 搜索项目后尝试从 RecyclerView 中删除项目时遇到问题,而如果我不使用 SearchView 并正常删除该项目,它工作正常。 这是一个示例,我正在删除该项目,
我正在尝试动态更改 RecycleView 中的图像。它将成功更改但是当我滚动 RecycleView ImageView 时将更改 这是我的适配器类代码: public class Recycler
我有一个表格如下 |GroupID | UserID | -------------------- |1 | 1 | |1 | 2 | |1 |
大家好,在我的 Activity 布局中,我使用这个 XML 来获取按钮数组
我有一个动画时钟,目前有内存泄漏。我目前似乎能够将东西放在图像之上,但不能将其取走或必须清除它(除了重新绘制整个时钟以使秒针不在两个不同的位置之外)。 用于清除时钟组件的代码是: for(UIView
我正在尝试创建一个简单的动画,在 JavaFX 2.x 中将圆向左移动一个像素,该动画有效,但每当我尝试第二次启动时,它就不再起作用了。我尝试了几种方法,代码如下: public void handl
我是一名优秀的程序员,十分优秀!