Backbone.js Memory Management, Rising DOM Node Count

时光总嘲笑我的痴心妄想 提交于 2019-11-28 19:35:18

I assumed that once the event listeners were properly disposed of that the DOM Node Count would just manage itself, but this doesn't seem to be the case.

If I got you right you are trying to dispose of the node by removing listeners from it, is that the case?

Note that adding event listener to a DOM node doesn't prevent the node from being garbage collected, the dependency is in opposite direction: while the node is alive the listener function will not be collected.

  • Is there a proper way to dispose of DOM Nodes so that they will be properly garbage collected, or is this DOM Node Count a running total that will never decrease?

To make sure a DOM node can be garbage collected you should

  1. Remove the node from the document tree.
  2. Clear all references from javascript to the node AND to all nodes in the same subtree as reference from javascript to one of the nodes in the subtree will hold whole subtree.

So it is not enough to just remove listeners from a node to make it collectable. Moreover, it is not necessary to remove listeners from a node if you want the node to be collected.

The DOM node count should be decreasing when some nodes are collected by GC and destroyed. The number indicates current amount of DOM nodes that have been created but not destroyed so it shouldn't grow indefinitely unless there is a memory leak.

  • Is the DOM Node Count even a reliable figure?

Yes. It should be a reliable figure as it is incremented whenever a new DOM node is created and decremented when it is destroyed. So the implementation is quite straightforward to trust it.

It's fixed! - UPGRADE BACKBONE. (continue reading)

We upgraded from Backbone 0.9.2 to Backbone 0.9.10 and implemented the listenTo and stopListening on every view/model/collection. The results are OMGFANTASTIC.

After 7 minutes of running the same stress-test, these are the results:

Results: 7.0 minutes, 6,926 DOM Nodes (Without the timeline recording) and the Event Listener Count looks like BLUE BLADES OF GRASS. I'm shocked. The memory usage is also amazingly low in comparison to previous tests.

After 18 Minutes: The event listener count is the same, never surpassing 154 and the DOM Node Count stays below 25,000 the entire time! There are obviously some things slipping by (some non-backbone components that are still in use, most-likely) but the improvement is astounding.

Conclusion: Prior to this version of Backbone, we were not doing a very good job cleaning up listeners within Backbone itself. The listeners to the DOM were handled fine, but not between models/views/collections. Many of the callbacks involved were tied to Backbone Views which I guess prevented the garbage collector from freeing up the DOM Nodes. Lots of straggling event listeners/callbacks within Backbone (not just binding to the DOM) create a lot of straggling DOM Nodes that can't be garbage collected.

If this isn't a good enough reason to upgrade Backbone, I don't know what is ;o

loislo

The rising DOM nodes count is the main sign of a memory leak (usually in the code of our page). So you need to fight against it. The standard technique is described in the answer to this question.

The snapshot content has too many details. And this 3 snapshot schema helps you to filter out not interesting part of the snapshot and shows only the candidates for a leak.

Please be sure that you are running the latest version of chrome, as example Chrome Canary. It should be a fresh instance with single tab without extensions. It would be nice to have no error messages in console, no breakpoints and do not stop on an exception because all these things may affect the page and as a result the snapshot content.

This post might be interesting for you too.

i found another way to avoid jank

render: function() {
  this.$el.empty();
  var container = document.createDocumentFragment();
  // render each subview, appending to our root element
  _.each(this._views, function(subview) {
     container.appendChild(subview.render().el)
  });
  this.$el.append(container);
}

refer it here http://ozkatz.github.io/avoiding-common-backbonejs-pitfalls.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!