1

I have a for loop that iterates over a fair few items.

At the start of each iteration I use the same var newObj = new ...

However, I'm wondering, is this completely replacing that object each time or should I be releasing it at the end of each iteration.

If I should release - how do I do release?

For example - say x is a large number (10.000 as a high example)

for (var x = 0; x<10000; x++) {
   var newObj = new someThing(x, y, z)
   newObj.dance()
   newObj.andDanceMore()

   //Should I do some kind of release here - or am I replacing it each time
}

Thanks.

Martijn
  • 15,791
  • 4
  • 36
  • 68
userMod2
  • 8,312
  • 13
  • 63
  • 115
  • 3
    The fact that you have to use `999999` in a loop is already a major red flag. – Abana Clara Feb 27 '18 at 08:15
  • 3
    `releasing it` - how would you do this? – Jaromanda X Feb 27 '18 at 08:17
  • you are rewriting the variable so I don't think you have to worry about memory – Arpit Solanki Feb 27 '18 at 08:17
  • May be you could split the situation in several chunks to be processed, but would be great to have a bit more of info on your objectives. Making "new" instances is always expensive. – juan garcia Feb 27 '18 at 08:19
  • @juangarcia Just an example/general question I had. Assuming it was just that and no other chunks i can seperate? – userMod2 Feb 27 '18 at 08:20
  • @Merigold - this is just a general thought. Not actually 999999. But i guess such a scenario could exist (maybe not as big as 999999) - e.g. if one was say checking 10000 items on an infinite scroll webpage?? – userMod2 Feb 27 '18 at 08:21
  • 2
    Don't ask "general" questions, that's pointless. Ask real questions about real, actual, reproducible problems you have. – Tomalak Feb 27 '18 at 08:21
  • For starters, use `let` instead of `var`. Let only exists within you for loop, not affecting the reset (after the loop is done) – Martijn Feb 27 '18 at 08:23
  • 2
    As 'Merigold' said, the fact that loop from 1 to 99999 is a problem itself. Anyway, you can use 'let' instead of 'var' and 'strict mode' to free up memory when the variable is not needed. https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare-a-variable – IgorM Feb 27 '18 at 08:24
  • @IgorM - Thanks that was the direction (let vs var) i needed. – userMod2 Feb 27 '18 at 08:27
  • @userMod2 depends a bit more on how specific is the situation, for instance you could see the implementation of PhaserJs for groups / collections where he have a lot of tweens, or you could think it as an stream where each instance needs to be transformed accordingly dancing ... is the need that makes you take a good decision at the end. – juan garcia Feb 27 '18 at 08:28

4 Answers4

1

in javascript you don't have to worry about "releasing" allocated memmory,

from the great MDN docs

High-level languages (such as JS) embed a piece of software called "garbage collector" whose job is to track memory allocation and use in order to find when a piece of allocated memory is not needed any longer in which case, it will automatically free it. This process is an approximation since the general problem of knowing whether some piece of memory is needed is undecidable (can't be solved by an algorithm).

basically the general behavior of the G.C is when an object has zero references to it - It can be garbage collected.

in the case you reffering to, each time you assigning a new value to var newObj, the G.C detect that there are 0 reffernces to it and garbage collected it - releasing it -

Ofir G
  • 736
  • 6
  • 18
0

Short answer: no.

Slightly longer answer: There is no manual memory management in JavaScript. It is a garbage collected language. Each time the GC is launched (and you have no control over when that happens), all objects that have no references will be automatically collected, effectively freeing the memory they occupied.

Further reading: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management

mingos
  • 23,778
  • 12
  • 70
  • 107
0

Do you know what Garbage Collector is? If not, you could find very nice blogposts googling "node.js garbage collection".

In brief:

1) When you override the variable value (or local variable is being dropped with its scope), the object still remains in memory.

2) When Node.js decides to run garbage collector, your object will be wiped out from memory in the following cases:

2.1) There are no variables, containing it (actually pointing to it).

2.2) There are some variables/properties pointing to it, but the entire cluster of objects and closures with mutual links is identified as isolated.

3) You may force garbage collector run (How to request the Garbage Collector in node.js to run?), but in regular cases you do not need this.

4) Difference between let and var, mentioned in comments, does not affect your case. Yes, let is more local than var, but what? If Node.js drops let variable quitting the loop, or you override any variable (var, let, property - whatever) with new value, the result is the same.

Pavel Koryagin
  • 1,479
  • 1
  • 13
  • 27
-1

var newObj Will be executed only once.What the program did is that Constantly assigned to newObj in the loop, objects that are not referenced will be recycled by GC.