Skip to content Skip to sidebar Skip to footer

Memory Leak In Javascript (chrome)

I'm calling a function 50 times a second, which does some expensive things as it is painting alot on a element. It works great, no problems there, but I just took a

Solution 1:

I'm just going to pull this quote directly, linked from the article;

Speaking of memory leaks, breaking circular references — the cause of the leaks — is usually done with simple null assignment. There’s usually no need to use delete. Moreover, null‘ing allows to “dereference” variables — what delete would normally not be able to do.

var el = document.getElementById('foo');
// circular reference is formed
el.onclick = function() { /* ... */ };
// circular reference is broken
el = null;
// can't `delete el` in this case, as `el` has DontDelete

For these reasons, it’s best to stick with null‘ing when breaking circular references.

delete Explained

Solution 2:

Look at heap profile under the Profiles tab in Chrome's developer tools for information about memory usage.

You can do the following to prevent memory leaks:

  • Test your code with JSLint, to see if that will give you some pointers.
  • Use the var keyword to give your variables function scope, so they can be garbage collected when they go out of scope. Without the var keyword variables have global scope.
  • Use delete variable; statements to remove the object as well as the reference from memory. Setting the variable to null will only remove the object from memory, but not its reference.

Post a Comment for "Memory Leak In Javascript (chrome)"