This question has been asked in a number of different forms, but I’m not sure my situation completely applies.
Say, I have a node.js program. My program connects to a Stream, by instantiating a class called Stream. Within Stream a StreamParser class is instantiated, which contains among other things a couple of Timers.
When I Stream.destroy() my original Stream, the object which holds the StreamParser is set to null. What will happen to the memory space, and what will happen to the timers? It seems my timers are still running, except when I explicitly clearTimeout them…
So, the nested structure:
new Stream()
-> this.stream = new StreamParser()
-> this.intv = setInterval(function() { // code }, 1000);
// Streams are destroyed like this:
Stream.destroy()
-> calls this.stream.destroy(function() {
self.stream = null;
// stream is null. but timers seem to be running. So, is stream still in memory?
}
I’m a bit confused. A bit more expanded code example:
// main call.
var stream = new Stream();
stream.connect();
setTimeout(function() {
stream.destroy(function() {
stream = null;
});
}, 60000);
/** ############# STREAM ############### **/
function Stream() {
this.stream = null;
this.end_callback = null;
}
Stream.prototype.connect = function() {
var self = this;
new StreamParser('stream.com', function(stream) {
self.stream = stream;
self.stream.on('destroy', function(resp) {
if(self.end_callback !== null && typeof self.end_callback === 'function') {
var fn = self.end_callback;
self.end_callback = null;
self.stream = null;
fn();
} else {
self.stream = null;
}
});
});
}
Stream.prototype.destroy = function(callback) {
this.end_callback = callback;
this.stream.destroy();
}
/** ############# STREAM PARSER ############### **/
function StreamParser(uri, callback) {
var self = this;
this.conn = null;
this.callback = null;
this.connectSocket(uri, function(conn) {
self.conn = conn;
self.callback(conn);
})
setInterval(function() {
self.checkHeartbeat();
}, 1000);
}
StreamParser.prototype.checkHeartbeat = function() {
// check if alive
}
StreamParser.prototype.destroy = function() {
this.conn.destroy();
this.emit('destroy', 'socket was destroyed');
}
In javascript, you don’t explicitly destroy an object like you do in languages like c++. Instead, when you clear a reference to that object by setting a variable to
null, that makes the object more eligible for garbage collection (there is now one less piece of script that holds a reference to the object). But, if there are any other references to the object, then it will still not be garbage collected.In this case, your timers hold a reference to your object in their closure (the function surrounding them) so that means that there is still a reference to the stream so it will not be cleaned up by the garbage collector. The system keep s a reference to your timers to they will execute normally (regardless of what you do with the stream object) and when they fire, they will operate on the (still present) stream object as they were designed to do.
If you want to stop the timers, then you need to explicitly use
clearTimeout()on them. That will cause the timer closure that contains the other reference to your stream object to then be released and, if there are no other references to your stream object anywhere else in your code, the javascript garbage collector will then be able to actually free up the memory used by the stream object.When the stream object is finally garbage collected, it will cause it’s references to any child objects that it refers to to no longer be active. If those objects themselves have no other references anywhere in your code pointing to them, then they will also be garbage collected. But, if some other code also points to one of those child objects, then the child object will be preserved for that other reference.