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

  • Home
  • SEARCH
  • 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 3458390
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:58:37+00:00 2026-05-18T09:58:37+00:00

How does garbage collection work in JavaScript? Is it similar to .NET garbage collection?

  • 0

How does garbage collection work in JavaScript? Is it similar to .NET garbage collection? And is it because the implementation of garbage collection in VBScript is bad that people avoided it and established a preference for JavaScript as their standard client-side language?

  • 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-05-18T09:58:38+00:00Added an answer on May 18, 2026 at 9:58 am

    How does garbage collection work?

    The short answer is: When a block of memory (an object, say) is no longer reachable, it is eligible to be reclaimed. When, how, or whether it is reclaimed is entirely up to the implementation, and different implementations do it differently. But at a language level, it’s automatic.

    For example:

    function foo() {
        var bar;
    
        bar = new ReallyMassiveObject();
        bar.someCall();
    }
    

    When foo returns, the object bar points to is automatically available for garbage collection because there is nothing left that has a reference to it.

    Contrast with:

    function foo() {
        var bar;
    
        bar = new ReallyMassiveObject();
        bar.someCall();
        return bar;
    }
    // elsewhere
    var b = foo();
    

    …now a reference to the object survives the call, and persists until/unless the caller assigns something else to b or b goes out of scope.

    Also contrast with:

    function foo() {
        var bar;
    
        bar = new ReallyMassiveObject();
        bar.someCall();
        setTimeout(function() {
            alert("Three seconds have passed");
        }, 3000);
    }
    

    Here, even after foo returns, the timer mechanism has a reference to the timer callback, and the timer callback — a closure — has a reference to the context where it was created, which in turn contains the bar variable. As a result, in theory, what bar refers to isn’t available for garbage collection immediately when foo returns. Instead, it’s kept around until the timer fires and releases its reference to the callback, making the callback and the context it refers to eligible for GC. (In practice, modern JavaScript engines can and do optimize closures where they can. For instance, in the above, static analysis shows the callback doesn’t refer to bar, and doesn’t contain any eval or new Function code that might refer to it dynamically at runtime, so the JavaScript engine can safely leave bar out of the context the function refers to, thus making what it refers to eligible for GC — and modern ones do). (More about closures in this article.)

    JavaScript has no problem handling cleaning up circular references, btw, so for instance:

    function foo() {
        var a, b;
    
        a = {};
        b = {};
        b.refa = a;
        a.refb = b;
    }
    

    When foo returns, the fact that a is referring to b and vice-versa isn’t a problem. Since nothing else refers to either of them, they can both get cleaned up. On IE, this is not true if one of the objects is a host-provided object (such as a DOM element or something created via new ActiveXObject) instead of a JavaScript object. (So for instance, if you put a JavaScript object reference on a DOM element and the JavaScript object refers back to the DOM element, they keep each other in memory even when no one is referencing either of them.) But that’s an IE bugissue, not a JavaScript thing.

    Re:

    is it because the vbscript GC is bad that people reverted to javascript as their standard client side api?

    JavaScript was the original client-side web scripting language. VBScript only came later, when Microsoft came out with a browser, and was only ever supported in Microsoft browsers. JavaScript was and is the only client-side scripting game in town if you want to work with the broadest range of browsers. <subjective>It’s also about eight times the language classic VBScript ever was. 😉 </subjective>

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

Sidebar

Related Questions

In a lay-man terminology how does the garbage collection mechanism work? How an object
It's well known that Apple does not provide automatic garbage collection on the iPhone
Does the iPhone support garbage collection? If it does, then what are the alternate
Once a controller object is created when does it become available for garbage collection?
Does the garbage collector clean up web service references or do I need to
Does the Java language have delegate features, similar to how C# has support for
Does anyone know how to get IntelliSense to work reliably when working in C/C++
Does anyone have any recommendations of tools that can be of assistance with moving
Does anyone use have a good regex library that they like to use? Most
As there is no garbage collection in Delphi, where exactly do you unload variables?

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.