I'm attempting to use Chrome Dev Tools to profile memory usage on my application and detect leaked DOM nodes and am puzzled by some behavior I'm seeing. According to the Chrome profiling docs, if I create DOM nodes and then properly release them, the tool should respond with a "Nodes" counter that resets to its baseline:
But if the sequence of actions is not expected to result in any retained memory, and the DOM node count does not drop down back to the baseline where you began, you have good reason to suspect there is a leak.
Methodology: I used an variation of the "Leaking DOM Nodes" example., modified to create large strings inside the nodes.
<html>
<head>
<script>
var leakedNodes = [];
var largeStr = new Array(1000000).join('x');
function createNode(text) {
var div = document.createElement("div"),
innerDiv = document.createElement("div"),
textNode = document.createTextNode(text + " - " + new Date().toTimeString() + "-" + largeStr);
innerDiv.appendChild(textNode);
div.appendChild(innerDiv);
return div;
}
function createLeakedNodes() {
var i;
for (i = 0; i < 200; i++) {
leakedNodes.push(createNode("Leaked:" + i));
}
}
function createGCNodes() {
var i;
for (i = 0; i < 200; i++) {
createNode("Collected:" + i);
}
}
function createNodes() {
createLeakedNodes();
createGCNodes();
}
function clearLeaks() {
leakedNodes = null;
}
</script>
</head>
<body>
<button onclick="createNodes()">Create</button>
<button onclick="clearLeaks()">Clear Leaks</button>
</body>
</html>
I launch the page in an incognito tab with no extensions enabled, launch the dev tools, and start the timeline. I push the "Create" button three times, then the "Clear Leaks" button once, then click the "Collect Garbage" button in the profiler to force a GC, wait for the GC to finish, then stop the timeline.
I look at the "Nodes" counter in the profiler, and expected to see it climb for each of the times I pressed the "Create" button, and then drop, either after I click the "Clear Leaks" button or after I forced GC. However, what I see instead is that the graph climbs, but never drops.
Is my understanding of the graph's behavior incorrect, or are the nodes legitimately not being released? If the latter, what code above is causing the leak, and why is the associated memory apparently being GCd as expected?