最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

garbage collection - Do javascript objects inside of an array erase from the memory when I clear the array? - Stack Overflow

programmeradmin3浏览0评论

I never really gave much thought to garbage collection and I don't know whether or not it is necessary to take into account when making small javascript games/applications. Any advice is appreciated, but I will ask my specific question at the end.

A lot of the time I write code of this form:

var foos=new Array();
generateFoos();
function generateFoos()
{
    foos=[];
    for (fooIndex=0;fooIndex<numberOfFoos;fooIndex++)
    {
        foos[fooIndex]=new foo(Math.random(),Math.random());
    }
} 
function foo(bar,bas)
{
   this.bar=bar;
   this.bas=bas;
}

So my question is, when I say foos=[] (line 5), does this delete the objects in that array from the memory or do they float around somewhere, making the program larger and slower? What should I do if I want to call generateFoos() a loooot of times, like every time the user presses a key.

Thanks!

I never really gave much thought to garbage collection and I don't know whether or not it is necessary to take into account when making small javascript games/applications. Any advice is appreciated, but I will ask my specific question at the end.

A lot of the time I write code of this form:

var foos=new Array();
generateFoos();
function generateFoos()
{
    foos=[];
    for (fooIndex=0;fooIndex<numberOfFoos;fooIndex++)
    {
        foos[fooIndex]=new foo(Math.random(),Math.random());
    }
} 
function foo(bar,bas)
{
   this.bar=bar;
   this.bas=bas;
}

So my question is, when I say foos=[] (line 5), does this delete the objects in that array from the memory or do they float around somewhere, making the program larger and slower? What should I do if I want to call generateFoos() a loooot of times, like every time the user presses a key.

Thanks!

Share Improve this question asked Jan 9, 2013 at 1:13 Nick ManningNick Manning 2,9891 gold badge35 silver badges53 bronze badges 6
  • foos[] is only existent in scope of the function - so if function code ends foos is deleted. – Bernie Commented Jan 9, 2013 at 1:24
  • 1 @Bernhard No, foos is declared at the very first line. – Lee Taylor Commented Jan 9, 2013 at 1:27
  • Why use delete instead of just clearing the array? – Nick Manning Commented Jan 9, 2013 at 1:39
  • @LeeTaylor Upps. ok so it will be existing until you delete the var - and so it's in the memory for that time – Bernie Commented Jan 9, 2013 at 1:39
  • 1 No you dont use delete for this. Delete removes a property from an object, it doesn't "delete" a variable. Please read about how delete works. – Geuis Commented Jan 9, 2013 at 1:49
 |  Show 1 more ment

2 Answers 2

Reset to default 7

For a specific answer, since the accepted one doesn't actually answer the question directly, is that yes, foo = [] does de-reference any previous values in the array.

As Ales says, "An object bees eligible for garbage collection when it bees unreachable." Indeed, this is when the browser will clear such things from memory.

An important point, delete DOES NOT GARBAGE COLLECT.

You see this over and over, and even in the ments on this question. The delete keyword removes a property from an object and has nothing to do with garbage collection.

I also wanted to offer some advice on your code itself.

1) Use literals, not new, for basic data types

2) Don't call functions before you declare them. Yes, it works but its messy and harder to read later. Remember, you spend much more time reading your code than writing it. Make it easy to follow later.

3) Remember your function scope. Any variable declared without var goes global. With var, it remains within the scope of the function that contains it. Another way variables are scoped within a function is when they are passed in as named parameters.

4) Use var on your functions when creating them. In your code, your functions are globals.

5) Use spacing. Density of text is not next to godliness. You might be 20-something now with great eyesight, but you'll appreciate white space in just a very few short years.

6) Declare counters in for loops with var. Unless you want them to be global. And you almost never will.

Lets re-work your code now:

var numberOfFoos = 10,
    foos = [];

var generateFoos = function(){

    foos = [];

    for( var fooIndex = 0; fooIndex < numberOfFoos; fooIndex++ ){

        foos[ fooIndex ] = new foo( Math.random(), Math.random() );

    }

},

foo = function( bar, bas ){

   this.bar = bar;
   this.bas = bas;

}

generateFoos();
console.log( foos );

To answer your question : An object bees eligible for garbage collection when it bees unreachable. If your program is not holding any other references to the objects in that array, they will be garbage collected.

The timing of the actual garbage collection depends on many aspects and on the choosen Garbage Collection algorithm but you should not worry about it when writing your code.

The best advice when considering Garbage collection is to leave the Garbage collector to do its job. Do not try to instruct it or help it (by e.g. nulling references manually). Focus on the problem and functional aspect of the code.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论