0

I am looking for some very basic examples of memory leaks in JavaScript and how to fix them. Specifically, if I do something like this in my code;

var obj = new MyObj();

Do I have to take care of something or do some steps explicitly so that obj does not leak? Or do I not have to worry about anything? Will it get garbage collected automatically by the browser ?

Are there any cross-browser differences in how memory handling is done (w.r.t. DOM or any other objects) ?

halfer
  • 19,824
  • 17
  • 99
  • 186
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
  • You can refer here with great examples present here . https://www.meteor.com/blog/2013/08/13/an-interesting-kind-of-javascript-memory-leak – maddy Mar 27 '15 at 09:42
  • You'll want to read [how does javascript garbage collection work?](http://stackoverflow.com/q/4324133/1048572) The aim of GC is that you don't have to worry. – Bergi Mar 27 '15 at 10:46

2 Answers2

0

I guess you can find the answer in this page Javascript Memory Management

Abhishek
  • 6,912
  • 14
  • 59
  • 85
bunnyshell
  • 253
  • 4
  • 12
0

A short summary: Objects will be garbage collected by the browser automatically when the object has no more references to it.

So doing

var obj = {}; obj = null;

will cause obj to be garbage collected at same random point in the future, you can not explicitly tell the browser to garbage collect this object. Off course this is something you will usually not be doing, since object references usually go out of scope.

Example:

function foo() {
 var obj = {};
}
foo();

After this function call, the obj variable doesn't exist anymore and goes out of scope, the object that was being referenced by obj is now a candidate to be garbage collected.

Robin-Hoodie
  • 4,886
  • 4
  • 30
  • 63
  • Thx...but if i do not explictly do obj = null ...then this would never get garbage collected ? – copenndthagen Mar 27 '15 at 09:52
  • It would, this is just explicitly setting the reference to null, when your reference goes out of scope it will also happen, see my updated post – Robin-Hoodie Mar 27 '15 at 09:58
  • Thx for the basic example...so most of the times, it seems there is no need for explicilty setting obj to null for GC...any simple example where this might not happen and we have to do explicitly ? – copenndthagen Mar 27 '15 at 10:04
  • I can not imagine any scenario where explicitly setting variables to null would be recommended, unless you have a function with loads (and I really do mean LOADS) of variables, and if you do have a function like that, there's probably a need to refactor this code. I myself have never had to explicitly set references to null. I would also suggest you read the article from the other poster to better understand memory management. – Robin-Hoodie Mar 27 '15 at 10:07
  • @RobinHellemans: It sometimes can be advisable when long-living closures have access to no-more needed, huge objects. But indeed, that is rare, and good compilers detect this case anyway. Use it only when you notice that the GC needs help (hints). – Bergi Mar 27 '15 at 10:49
  • @Bergi Correct, I never thought of that before, thanks for the tip :) – Robin-Hoodie Mar 27 '15 at 10:51