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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:10:06+00:00 2026-05-18T20:10:06+00:00

I am looking at the example from http://www.javascriptkit.com/javatutors/oopjs.shtml var person = new Object() person.name

  • 0

I am looking at the example from http://www.javascriptkit.com/javatutors/oopjs.shtml

var person = new Object()
person.name = "Tim Scarfe"
person.height = "6Ft"

But there is no mention how to “free” it in order to avoid memory leak.

Will the following code free it?

person = null;
  1. How do you free a JavaScript Object using “new Object()?
  2. How do you free a JavaScript Array allocated using “new Array(10)”?
  3. How do you free a JavaScript JSON allocated using “var json = {“width”: 480, “height”: 640}”?

Thanks in advance for your help.

  • 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-18T20:10:06+00:00Added an answer on May 18, 2026 at 8:10 pm

    You don’t have to explicitly "free" JavaScript objects. All standard JavaScript hosts/environments use garbage collection based on whether the object can be reached anymore. (There may be niche hosts, such as some for embedded systems, that don’t; they’ll supply their own means of explicitly releasing things if so.) If the object can’t be reached anymore, the memory for it can be reclaimed.

    What you can do is ensure that nothing is referencing memory you’re not using any more, since memory that is being referenced cannot be released. Nearly all of the time, that happens automatically. For instance:

    function foo() {
       var a = [1, 2, 3, 4, 5, 6];
    
       // Do something
    }
    

    The memory allocated to the array a pointed to is eligible to be reclaimed once foo returns, because it’s no longer referenced by anything (a having gone out of scope with nothing having an outstanding reference to it).

    In contrast:

    function foo() {
       var a = [1, 2, 3, 4, 5, 6];
    
       document.getElementById("foo").addEventListener("click", function() {
           alert("a.length is " + a.length);
       });
    }
    

    Now, the memory that a points to cannot be reclaimed, because there’s a closure (the event handler function) that has an active reference to it, and there’s something keeping the closure in memory (the DOM element).

    You might think that only matters in the above, where the closure clearly uses a, but wouldn’t matter here where it doesn’t:

    function foo() {
       var a = [1, 2, 3, 4, 5, 6];
    
       document.getElementById("foo").addEventListener("click", function() {
           alert("You clicked foo!");
       });
    }
    

    But, per specification a is retained even if the closure doesn’t use it, the closure still has an indirect reference to it. (More in my [fairly old] blog post Closures Are Not Complicated.) Sometimes JavaScript engines can optimize a away, though early aggressive efforts to do it were rolled back — at least in V8 — because the analysis required to do it impacted performance more than just having the array stay in memory did.

    If I know that array isn’t going to be used by the closure, I can ensure that the array isn’t referenced by assigning a different value to a:

    function foo() {
       var a = [1, 2, 3, 4, 5, 6];
    
       document.getElementById("foo").addEventListener("click", function() {
           alert("You clicked foo!");
       });
    
       a = undefined; // <===============
    }
    

    Now, although a (the variable) still exists, it no longer refers to the array, so the array’s memory can be reclaimed.

    More in this other answer here on StackOverflow.


    Update: I probably should have mentioned delete, although it doesn’t apply to the precise code in your question.

    If you’re used to some other languages, you might think "Ah, delete is the counterpart to new" but in fact the two have absolutely nothing to do with one another.

    delete is used to remove properties from objects. It doesn’t apply to your code sample for the simple reason that you can’t delete vars. But that doesn’t mean that it doesn’t relate to other code you might run across.

    Let’s consider two bits of code that seem to do largely the same thing:

    var a = {};         // {} is the same as new Object()
    a.prop = "foo";     // Now `a` has a property called `prop`, with the value "foo"
    a.prop = undefined; // Now `a` has a property called `prop`, with the value `undefined`
    

    vs.

    var b = {};         // Another blank object
    b.prop = "foo";     // Now `b` has a property called `prop`, with the value "foo"
    delete b.prop;      // Now `b` has *NO* property called `prop`, at all
    

    Both of those make the memory that prop was pointing to eligible for garbage collection, but there’s a difference: In the first example, we haven’t removed the property, but we’ve set its value to undefined. In the second example, we’ve completely removed the property from the object. This is not a distinction without a difference:

    alert("prop" in a); // "true"
    alert("prop" in b); // "false"
    

    But this applies to your question in the sense that deleting a property means any memory that property was pointing to becomes available for reclamation.

    So why doesn’t delete apply to your code? Because your person is:

    var person;
    

    Variables declared with var are properties of an object, but they cannot be deleted. ("They’re properties of an object?" I hear you say. Yes. If you have a var at global scope, it becomes a property of the global object [window, in browsers]. If you have a var at function scope, it becomes a property of an invisible — but very real — object called a "variable object" that’s used for that call to that function. Either way, though, you can’t delete ’em. More about that in the link above about closures.)

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

Sidebar

Related Questions

I am looking at the example from http://www.javascriptkit.com/javatutors/oopjs.shtml var person = new Object() person.name
Here is an example of polymorphism from http://www.cplusplus.com/doc/tutorial/polymorphism.html (edited for readability): // abstract base
Using Bill Burrows Intro to SL4 and WCF Ria Services - Golf Example (http://www.myvbprof.com/MainSite/index.aspx#/zSL4_RIA_01),
I'm looking for an example of how to load an image from file and
I am looking for a C# example implementation of a class derived from Microsoft's
I am really new to Python and I have been looking for an example
my UIWebView is behaving strangely. For example, if I go from http://google.co.uk to http://ftd.de
I adapted this tutorial (http://www.screaming-penguin.com/node/7749) to an Android app I've built to allow for
Looking for an example that: Launches an EXE Waits for the EXE to finish.
I'm looking for an example of a TCustomDataSet implementation in C++ beyond the TTextDataset

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.