Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8360479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:28:37+00:00 2026-06-09T11:28:37+00:00

This question has been asked in a number of different forms, but I’m not

  • 0

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');
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:28:38+00:00Added an answer on June 9, 2026 at 11:28 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been asked in various forms in a number of different forums,
I believe that this question has been asked in a few different forms, but
This question has been asked before somewhat, but I hope mine differs. My situation
I know this question has been asked a number of different ways, and I
I know this question has been asked before but the solutions did not work
This question has been asked before ( link ) but I have slightly different
This question has been asked here in one form or another but not quite
Apologies if this question has already been asked but I do not think I
I am not sure if this question has been asked here or not. I
This question has been asked before but I am facing a slightly different problem.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.