6

I made an HTML5 game that consists of many small levels. When the player get's to the doors, another level is loaded. When a level is loading it basically just sets all the instance arrays to [] and then pushes stuff into them, by creating new instances of things, for example:

enemies = [] //this has previously been full of pointers from the old level
for (i = 0; i < n_enemies; i ++)
    enemies.push(new Enemy());

But, it has come to my attention that merely setting an array full of pointers to [], doesn't actually delete the instances! So, does javascript do this automatically? Or do I have to delete each instance myself?

corazza
  • 31,222
  • 37
  • 115
  • 186

4 Answers4

3

If the objects that were in the array are no longer referenced from anywhere then they will be garbage collected. There's no specification that states when this will occur, but it should be soon after removing them from the Array.

This should not present a memory leak.

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
Jivings
  • 22,834
  • 6
  • 60
  • 101
  • 2
    Also `delete` is only defined for properties of objects, so javascript doesn't even contain a way of deleting array variables - you have to just assume the js engine will take care of the garbage collection after all references are removed. – wheresrhys Mar 05 '12 at 13:28
  • wheresrhys - Array members are just properties with numeric names. Delete works on them too (e.g. `delete array['0']` removes the first member of an array starting at zero), but as Jivings says, it's unnecessary here. – RobG Mar 05 '12 at 13:47
  • @RobG - I probably should've been clearer - by "array variables" I meant variables that are arrays, rather than primitive values stored in arrays. – wheresrhys Mar 05 '12 at 14:24
2

I dont know much about developing games but normally in Javascript clearing Array is done like this, and this is good practice

enemies.length = 0;

check this post

Community
  • 1
  • 1
FosterZ
  • 3,863
  • 6
  • 38
  • 62
  • I've never heard of this before. It appears to work (just tested in Chrome) but any reference to this being good practice? – Davy8 Mar 05 '12 at 13:17
  • OK, but I believe that this should be a comment, as it doesn't answer the question directly. Thanks. I tested it as well in Chrome, it works, but I didn't read about it anywhere. If it is the "correct" way, why aren't more people talking about it? – corazza Mar 05 '12 at 13:18
  • 2
    `[]` This won't empty the array, it'll create a new empty array – FosterZ Mar 05 '12 at 13:18
  • check out that post i mentioned above, people do use this man, i use this a lot of time.. – FosterZ Mar 05 '12 at 13:25
  • @FosterZ `it'll create a new empty array` - There's no difference. – Jivings Mar 05 '12 at 13:37
  • see if that array previously contains reference those will be uneffected while `You can set the length property to truncate an array at any time`, https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/length – FosterZ Mar 05 '12 at 13:41
  • http://www.hunlock.com/blogs/Mastering_Javascript_Arrays here the reference that says `myArray = []` is the best practice to create a **new array**, now here the question is to clear the existing array. – FosterZ Mar 05 '12 at 13:44
  • 2
    @Jivings - there is a huge difference `a = []` creates and assigns a new array to `a`, **leaving the original array untouched**, `a.length = 0` sets the length of the array to zero (effectively deleting all its numeric properties). – RobG Mar 05 '12 at 13:51
  • 1
    @Jivings to be more clear as to RobG's point. `var a = [1,2,3];` `var b = a;` If you use `a = [];` then `b` is still `[1,2,3]`. If you use `a.length = 0;` Then both `a` and `b` are cleared (because they're still both pointing to the same array, whereas if you used `a = [];` there would now be 2 arrays `[]` and `[1,2,3]`) – Davy8 Mar 05 '12 at 13:59
1

It's like any other programming language. If there is a reference to object it won't be deleted.

e.g.

enemies = [];
enemy = new Enemy();
enemies.push(enemy);
enemies = [];

In case you don't create a reference to object after emptying enemies, enemy object will also be deleted

enemies.push(new Enemy());
Ivan
  • 2,262
  • 1
  • 18
  • 16
  • *any other **garbage collected** programming language. If you're working in a non-GC'd language, you'll need to handle memory allocation yourself. – Davy8 Mar 05 '12 at 13:16
1

There is no free command in JavaScript, so you can't actually "free" any memory yourself. All you can do is: Kill all references (pointers) to some object. Eventually, the garbage collector will look for objects that are no longer visible to anyone.

Or rather: The GC will eventually collect all memory which can still be reached and forgets about the rest. This is why only live objects cost in a GC environment.

But there is one catch: The GC doesn't tell objects that they are dead. So if your Enemy object needs some cleanup, then you must do that manually.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • There isn't? I'm pretty sure there is, it is a keyword and I read about it somewhere! +1 for all the useful info. – corazza Mar 05 '12 at 13:21
  • 1
    @Bane [The delete operator](https://developer.mozilla.org/en/JavaScript/Reference/Operators/delete) – Jivings Mar 05 '12 at 13:34
  • The `delete` operator removes a property from an object; it doesn't free memory as such. Think of it as the counterpart of `Array.pop()` – Aaron Digulla Mar 05 '12 at 15:21