Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freeing memory used by unattached DOM nodes in Javascript

As part of my application, I'm putting together a set of small Dom nodes that are not shown all at once. I'm storing them in an internal array. The user can invoke their display in which case I reparent them to the div that is used to display them. That's all well and good. But when it's time to replace all of them with new ones, I want to destroy the old ones (effectively deallocate them). Otherwise, over time, memory usage could grow exponentially. How do I force the browser js engine to do this? Is just setting each of the items in my array of Dom nodes to null enough? Is there something else I have to do? Or maybe I don't have to worry about this at all?

like image 655
Karim Avatar asked Feb 08 '09 18:02

Karim


People also ask

How to avoid memory leaks in JavaScript?

setTimeout and setInterval are the two timing events available in JavaScript. The setTimeout function executes when the given time is elapsed, whereas setInterval executes repeatedly for the given time interval. These timers are the most common cause of memory leaks.

What are detached DOM nodes?

Detached DOM elements are the elements which have been removed from the DOM but their memory is still retained because of JavaScript. This means that as long the element have a reference to any variable or an object anywhere, it does not garbage collected even after destroyed from the DOM.


1 Answers

If you set each item to null, they will be automatically garbage collected.

like image 200
Kevin Babcock Avatar answered Sep 28 '22 05:09

Kevin Babcock