Backbone.js的内存管理,瑞星DOM节点计数 [英] Backbone.js Memory Management, Rising DOM Node Count

查看:107
本文介绍了Backbone.js的内存管理,瑞星DOM节点计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

状况:我工作的一个pretty体面复杂的单页骨干的应用程序,可能被直行8-12 +小时。正因为如此,有必要确保应用程序不会泄露,并有声誉X小时后崩溃或显着放缓。

应用:这个应用程序是建立在骨干(MV *)的Zepto (类似jQuery的),卷曲(AMD装载机)及胡子(模板)。

问题:我刚刚征服了事件侦听器。垃圾收集器似乎是在做了很好的工作清理这些家伙了,但是DOM节点计数不会停止攀登。

问题


  • 是否有这样他们会得到妥善垃圾回收处置DOM节点的一个适当的方式,或者是这个DOM节点计数的运行总计永远不会减少?

  • 有谁知道任何这些框架不佳手柄DOM节点?可能是胡子?

  • 是DOM节点计数连可靠的数字?

我真的只是寻找一个良好的开端在我的冒险,从上升停止这些DOM节点。任何帮助或指导将大大AP preciated(并相应upvoted)。

我认为,一旦事件侦听器进行妥善处置了DOM节点计数只想自己管理自己,但这似乎并不如此。

测试



  • 第一个测试:6.8分钟,110 DOM节点


修改:如果没有时间轴记录,我重新运行相同的脚本随机捣烂环节,并采取了屏幕截图在大约7分钟大关。通过GC来了之后,我把这些结果。


  • 第二个测试 7.1分钟,141,000 DOM节点(不时间轴记录)


编辑:修正后


升级后骨干,并使用listenTo和处处的stopListening


  • 7分钟:6,926 DOM节点(见下面标明的答案)

  • 20分:6000 DOM节点,20事件监听器,内存20 MB

  • 25分:11600 DOM节点,44侦听器,内存21.7 MB

  • 28分钟:9,000 DOM节点,22事件监听器,内存21.7 MB

  • :13700 DOM节点,123事件监听器,内存21.7

  • 31分钟:7,040 DOM节点,30侦听器,内存21.7


解决方案

  

我认为,一旦事件侦听器进行妥善处置了DOM节点计数只想自己管理自己,但这似乎并不如此。


如果我得到你的权利,你正试图从它删除听众处置的节点,是这样吗?

请注意,添加事件侦听器DOM节点并不prevent被垃圾收集节点,依赖是在相反的方向:当节点是活着监听功能将不会被收集


  

      
  • 是否有处置DOM节点的正确方法,使他们正确地垃圾回收,或者是这个DOM节点计数的运行总计将永远不会下降?

  •   

要确保DOM节点可以被垃圾收集,你应该


  1. 从文档树的节点。

  2. 清除从JavaScript到节点,即可在同一子树从javascript参考所有节点的子树的一个节点的所有引用将持有整个子树。

因此​​,它是不够的,只是从节点删除监听器,使其收藏价值。此外,它的不需要如果您想收集的节点从节点删除监听器。

在DOM节点数量应该是当一些节点通过GC收缴和销毁下降。该数字表示已创建,但不被破坏,除非是有内存泄漏不应该无限期地增长DOM节点的电流量。


  

      
  • 是DOM节点计数连可靠的数字?

  •   

是的。它应该是一个可靠的数字作为每当创建了新的DOM节点它被递增,当它被破坏递减。因此,实现非常简单信任它。

Situation: I'm working on a pretty decently complex single page Backbone app that could potentially be running for 8-12+ hours straight. Because of this, there's a need to ensure that the application won't leak and have a reputation for crashing after X hours or slow down dramatically.

The Application: The app is built on Backbone (mv*), Zepto (similar to jquery), Curl (amd loader) & Mustache (templating).

Problem: I've just conquered the event listeners. The garbage collector seems to be doing a fine job cleaning these guys up, but the DOM Node Count won't stop climbing.

Questions:

  • 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?
  • Does anybody know of any of these frameworks to poorly handle DOM Nodes? Possibly Mustache?
  • Is the DOM Node Count even a reliable figure?

I'm really just looking for a head start on my adventure to stop these DOM Nodes from rising. Any help or guidance would be greatly appreciated (and accordingly upvoted).

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.

Tests


  • First Test: 6.8 minutes, 110,000 DOM Nodes

Edit: Without the Timeline recording, I reran the same script to randomly mash links and took a screenshot at around the 7 minute mark. After GC came through I had these results.

  • Second Test: 7.1 minutes, 141,000 DOM Nodes (Without the timeline recording)

Edit: After Fix:

After Upgrade Backbone and using listenTo and stopListening everywhere

  • 7 minutes: 6,926 DOM Nodes (see marked answer below).
  • 20 minutes: 6,000 DOM Nodes, 20 Event Listeners, Memory 20 MB.
  • 25 minutes: 11,600 DOM Nodes, 44 Listeners, Memory 21.7 MB.
  • 28 minutes: 9,000 DOM Nodes, 22 Event Listeners, Memory 21.7 MB.
  • 30 minutes: 13,700 DOM Nodes, 123 Event Listeners, Memory 21.7.
  • 31 minutes: 7,040 DOM Nodes, 30 Listeners, Memory 21.7.

解决方案

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.

这篇关于Backbone.js的内存管理,瑞星DOM节点计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆