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 8684097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:12:54+00:00 2026-06-12T22:12:54+00:00

From what I understand, the JS garbage collector removes objects that are no longer

  • 0

From what I understand, the JS garbage collector removes objects that are no longer referenced. Suppose I remove a reference to an object and that object has a property which is a reference to another object. Will both objects be removed?

e.g

var objectA = {
    prop1: "property",
    prop2: "property"
}

var objectB = {
    refToA: objectA
}

// Remove reference to objectA
delete objectA;

// Remove reference to objectB
delete objectB

Are both objects now completely removed from memory?

  • 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-12T22:12:55+00:00Added an answer on June 12, 2026 at 10:12 pm

    Short answer: Yes. An object that isn’t referenced anywhere is completely removed from memory, regardless of what is referenced by that objects properties.

    In your snippet, things are fairly straightforward, though mem-management can get tricky when you start using closures:

    var objA = (function()
    {
        var objAb = {who:'An object literal in closure scope',globalReference:'None!'};
        return {who:'Return value, is an object literal that references a closure object'.
                closureObj : objAb};
    })();
    var objB = {who:'some object',objAb:{who:'object literal, referenced by objB.objAb'}};
    var objBAb = objB.objAb;//reference to obj literal, referenced by objB.objAb
    var objAb = objA.closureObj;//reference to obj literal, referenced by objA.closureObj, which in turn references the closure object literal
    
    delete objB.objAb;
    console.log(objBAb);//<-- the object literal that was declared as a property of objB still exists
    delete objAb;//<-- this reference is gone, but the closure obj still exists 
    console.log(objA.closureObj);//<-- object is there
    

    Basically, objects are nameless entities. The variables used to access them are references, the never, ever contain the actual object itself. That floats around in JS-space. When you use delete someVar, all you do is unset the actual value of that variable, which is a memory address (sort of).

    If the JS GC can’t find any variables that reference a location in memory that contains an object, it will reclaim that memory.
    It’s as simple as that.

    When you apply this logic to the following code:

    var objA = (function()
    {
        var closureObj = {iam:'An object literal defined inside a closure scope'};
        var functionsAreObjects = function()
        {//nameless function object, inside closure scope, too
            return closureObj;
        };
        var resetFunction = function()
        {
            this.closureReference = functionsAreObjects();//assign return value to this
        };
        return {iam:'The returned object literal',
                closureReference:closureObj,
                reset:resetFunction,
                getClosureReference:functionsAreObjects};
    })();
    delete objA.closureReference;//the closure object IS NOT GC'ed
    

    In the previous example, the last delete statement would haven sufficed to GC the closure object literal. However, now objA has two methods (properties that reference function objects). These methods still reference the closure object, and are still referenced by objA, so the closureObj can’t be GC’ed yet. So, this is where things get tricky:

    delete objA.closureReference;
    delete objA.reset;
    delete objA.getClosureReference;
    

    We’ve deleted all properties that can link back to the closureObj, so it stands to reason that it’ll be GC’ed, right? –Erm, not quite. Chrome’s V8 does deallocate the memory, but I’ve heard of similar code causing leaks in Opera, and it’s not out of the realm of possibility’s that perhaps IE will not excel at reclaiming the memory.

    Also, we’ve effectively created a getter method with getClosureReference, so in real life, this is very likely to occur:

    //do stuff
    delete objA.closureReference;
    var objB = objA.getClosureReference();//<-- created new reference to closure object
    //do some more stuff
    delete objA.reset;
    delete objA.getClosureReference;
    

    In this case, the closureObj can’t be GC’ed, because it’s still being referenced by objB somewhere. Only when that variable goes out of scope will closureObj be deallocated. Not only is this a very solid argument against global variables (they never go out of scope, and thus never get GC’ed), it also goes to show that closures, neat though they are, require some more overhead from the developer: letting a variable go out of scope doesn’t necessarily mean the memory is freed: some closure could expose a reference to an object, or a function that references that object…

    I’ve posted a question on this matter a while ago but it might explain a few things.
    And just for fun, if you want an example of nested closures (closers in closures, in closures, passing references to each other, and other objects) try finding out when what can be GC’ed in the following code

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

Sidebar

Related Questions

When designing a class that has a reference to another object it might be
I found that Maven implies specific directory layout. But I don't understand from here:
From what I understand REE is better in terms of garbage collection and much
I can see this curious behaviour from the garbage collector public class A {
My objects in a complex structure have a property Dictionary<Object, Object> Annotations , where
As you can understand from title, i modified Settings.cs file.Added some properties, some code
As far as I understand from the documentation the QUdpSocket are async but, still,
From what I understand IMAP requires a connection per each user. I'm writing an
From what I understand using $this->db->insert() escapes the values: http://codeigniter.com/user_guide/database/active_record.html#insert Note: All values are
From what I understand, when a socket buffer (skb) is allocated by the kernel

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.