I am using Node.js
. I have an array of objects
.
我正在使用Node.js.我有一个对象数组。
var my_array = [];
var param_array = [xx, xx, ...];
for (i = 0; i < 5; i++) {
my_array[i] = new ClassName(param[i]);
(my_array[i]).on('event_name', function() {
// do something
});
}
All the objects are created from a class/library. All the objects have event listener
.
所有对象都是从类/库创建的。所有对象都有事件监听器。
After finished using the objects in the array, I want to delete all the objects and free up memory.
在完成使用数组中的对象后,我想删除所有对象并释放内存。
Based on Google search results, the Garbage Collector
should automatically free up memory when the memory is not referenced by any variable anymore. So, I set the array to a new empty array:
根据Google搜索结果,当内存未被任何变量引用时,垃圾收集器应自动释放内存。所以,我将数组设置为一个新的空数组:
my_array = [];
However, I was surprised to find that all the event listeners
of the objects in the old array are still responding to events
.
但是,我很惊讶地发现旧数组中对象的所有事件侦听器仍然响应事件。
How can I delete (or dereference) all the objects which have event listener and free up memory?
如何删除(或取消引用)具有事件侦听器和释放内存的所有对象?
1 个解决方案
#1
2
You are only clearing your array. There may be a bug in your code, can't tell from code you posted. To be sure, you should iterate over your my_arrays
objects and call
你只是清理阵列。您的代码中可能存在错误,无法通过您发布的代码判断出来。当然,您应该迭代my_arrays对象并调用
removeListener (‘event_name’, eventListener)
removeListener('event_name',eventListener)
for each, then clear my_array
using my_array = []
. In that way Garbage Collector
will take care of those, as there will be no reference. First argument is event name, second an eventListener you want to delete.
对于每个,然后使用my_array = []清除my_array。通过这种方式,垃圾收集器将处理这些,因为没有参考。第一个参数是事件名称,第二个是要删除的eventListener。
Also, in your case, you should use
另外,在你的情况下,你应该使用
removeAllListeners(‘event_name’)
as it doesn't need eventListener function name and you don't need those objects anymore later.
因为它不需要eventListener函数名称,您以后不再需要这些对象。
SSCCE:
//creating an EventEmitter object just to illustrate how it works
var events = require('events');
function ExampleObject() {
events.EventEmitter.call(this);
this.talk = function()
{
this.emit('event_name');
}
}
ExampleObject.prototype.__proto__ = events.EventEmitter.prototype;
var exampleObject = new ExampleObject();
exampleObject.on('event_name', function() {
console.log('Here I go!');
});
exampleObject.talk();
//---------
my_array = [];
var listener = function listener()
{
console.log('event_name_listener');
}
for(i = 0; i < 5; i++)
{
my_array[i] = new events.EventEmitter();
my_array[i].on('event_name', listener);
}
for(i = 0; i < my_array.length; i++)
{
my_array[i].emit('event_name');
console.log(i);
}
for(i = 0; i < my_array.length; i++)
{
my_array[i].removeAllListeners('event_name'); //could use removeListener('event_name', listener)
}
my_array = [];
console.log("Array length:"+my_array.length);
//------------- and here our ExampleObject
exampleObject.talk();
console.log("Listeners: "+require('util').inspect(exampleObject.listeners('event_name')));
console.log(exampleObject);//more info
exampleObject.removeAllListeners('event_name');
exampleObject.talk();//nothing happens
console.log("Listeners: "+require('util').inspect(exampleObject.listeners('event_name')));
delete exampleObject;
console.log(exampleObject);
Output:
Here I go!
event_name_listener
0
event_name_listener
1
event_name_listener
2
event_name_listener
3
event_name_listener
4
Array length:0
Here I go!
Listeners: [ [Function] ]
ExampleObject {
domain: null,
_events: { event_name: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
talk: [Function] }
Listeners: []
ExampleObject {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
talk: [Function] }
At the end all eventListeners are deleted.
最后,删除所有eventListeners。
Also, if this helps you, remember to accept this answer.
此外,如果这有助于您,请记住接受此答案。
#1
2
You are only clearing your array. There may be a bug in your code, can't tell from code you posted. To be sure, you should iterate over your my_arrays
objects and call
你只是清理阵列。您的代码中可能存在错误,无法通过您发布的代码判断出来。当然,您应该迭代my_arrays对象并调用
removeListener (‘event_name’, eventListener)
removeListener('event_name',eventListener)
for each, then clear my_array
using my_array = []
. In that way Garbage Collector
will take care of those, as there will be no reference. First argument is event name, second an eventListener you want to delete.
对于每个,然后使用my_array = []清除my_array。通过这种方式,垃圾收集器将处理这些,因为没有参考。第一个参数是事件名称,第二个是要删除的eventListener。
Also, in your case, you should use
另外,在你的情况下,你应该使用
removeAllListeners(‘event_name’)
as it doesn't need eventListener function name and you don't need those objects anymore later.
因为它不需要eventListener函数名称,您以后不再需要这些对象。
SSCCE:
//creating an EventEmitter object just to illustrate how it works
var events = require('events');
function ExampleObject() {
events.EventEmitter.call(this);
this.talk = function()
{
this.emit('event_name');
}
}
ExampleObject.prototype.__proto__ = events.EventEmitter.prototype;
var exampleObject = new ExampleObject();
exampleObject.on('event_name', function() {
console.log('Here I go!');
});
exampleObject.talk();
//---------
my_array = [];
var listener = function listener()
{
console.log('event_name_listener');
}
for(i = 0; i < 5; i++)
{
my_array[i] = new events.EventEmitter();
my_array[i].on('event_name', listener);
}
for(i = 0; i < my_array.length; i++)
{
my_array[i].emit('event_name');
console.log(i);
}
for(i = 0; i < my_array.length; i++)
{
my_array[i].removeAllListeners('event_name'); //could use removeListener('event_name', listener)
}
my_array = [];
console.log("Array length:"+my_array.length);
//------------- and here our ExampleObject
exampleObject.talk();
console.log("Listeners: "+require('util').inspect(exampleObject.listeners('event_name')));
console.log(exampleObject);//more info
exampleObject.removeAllListeners('event_name');
exampleObject.talk();//nothing happens
console.log("Listeners: "+require('util').inspect(exampleObject.listeners('event_name')));
delete exampleObject;
console.log(exampleObject);
Output:
Here I go!
event_name_listener
0
event_name_listener
1
event_name_listener
2
event_name_listener
3
event_name_listener
4
Array length:0
Here I go!
Listeners: [ [Function] ]
ExampleObject {
domain: null,
_events: { event_name: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
talk: [Function] }
Listeners: []
ExampleObject {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
talk: [Function] }
At the end all eventListeners are deleted.
最后,删除所有eventListeners。
Also, if this helps you, remember to accept this answer.
此外,如果这有助于您,请记住接受此答案。