score:19

Accepted answer

i think you have your answer already but i was interested in how you show that this is true, at least in latest chrome.

this is the section of the d3 code that removes dom nodes:

function remove() {
  var parent = this.parentnode;
  if (parent) parent.removechild(this);
}

export default function() {
  return this.each(remove);
}

so as you can see it's depending on the browser to do cleanup of any associated listeners.

i created a simple stress test of adding/removing lots of circle nodes with d3:

  var circles = svg.selectall("circle")
    .data(data, function(d) { return d.id; } );

  circles.exit().remove();

  circles.enter().append("circle")
    .attr("id", function(d) { return d.id; })
    .attr("cx", function(d) { return d.x; })
    .attr("cy", function(d) { return d.y; })
    .attr( { r: 5, fill: 'blue' })
    .on("mouseenter", function(d) { console.log('mouse enter') });    

live version here: http://bl.ocks.org/explunit/6413685

  1. open the above with latest chrome
  2. open the developer tools
  3. click the timeline tab
  4. click the record button at the bottom
  5. let it run for a couple minutes, then click the button again to stop recording
  6. drag the selector in the top timeline view to cover several of the garbage collection sawtooth patterns

you will notice that the dom node garbage collection counts correspond with the event listener garbage collection counts. in fact, you can't really tell them apart in the below screenshot since the lines are superimposed:

chrome screenshot

note that for internet explorer, things are a little more complicated.

see also this article for more tips on tracking memory usage in chrome tools.


Related Query

More Query from same tag