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?
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.
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.
If you set each item to null, they will be automatically garbage collected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With