I am working on an application that is creating and removing a lot of DOMs. I've notice that the process memory from the browser tab continuously increases, despite the javascript heap memory remaining constant. In a test application I create and remove divs from a parent div.
<button onclick="createStuff()">Create</button> <button onclick="deleteStuff()">Delete</button> <div id="parent"></div> function createStuff() { var parentDiv = document.getElementById('parent'); for (var i = 0; i < 50000; i++) { var child = document.createElement('div'); child.id = i; child.textContent = i; parentDiv.appendChild(child); child = null; } parentDiv = null; } function deleteStuff() { var parentDiv = document.getElementById('parent'); for (var i = 0; i < 50000; i++) { var child = document.getElementById(i); parentDiv.removeChild(child); child = null; } parentDiv = null; } I've confirmed that the javascript heap is not leaking with the chrome dev tools (I'm new to them so I could have missed something). However the memory for the process continues to increase. From everything I've read I suspect that the removed doms are still in the dom heap.
Other posts also say that the browser will eventually free the memory allocated to the removed doms. In the above jsfiddle example I've hit create and delete several times. My javascript heap is steady at 4.9MB. My process memory is up to 115MB. I've waited 30 mins and it hasn't gone down at all.
Questions
- When are removed DOM elements completely removed from the browser process memory?
- Is there a way to force DOM garbage collection?
- Is there a tool to get more insight into what doms are marked for garbage collection? I couldn't find one in Chrome or IE.
Thanks for the help!
Edit
I have used the chrome dev tools and the javascript heap is not growing. Interestingly, the only thing that changes between the heap snapshots is an (array) object. It's my understanding that anything in parenthesis is controlled by the browser and outside of my reach. Each subsequent create->delete removes the old (array) object and creates a new one during the delete.
In timeline I can see that the javascript heap is constant and the nodes get cleaned up, but the memory as shown with (shift + esc) never goes down even after the node count drops.

It seems like I'm doing everything I can to make sure I cleanup my javascript heap, but the dom cleanup is out my reach and independent of the javascript GC. Is this statement correct?
Are the removed doms part of the young generation heap? Is there a way to set a limit on this heap size? I repeated the test until I had reached 500MB and still no cleanup. I'm using Chrome 35.0.1916.114 btw.
nullout local variables. They will be disposed of automatically when the function completes (as long as there isn't something creating a closure that keeps the scope alive - which there isn't in your two functions).