Javascript + Knockout.js内存泄漏-如何确保对象被销毁? [英] Javascript + Knockout.js memory leak - How to make sure object is being destroyed?

查看:126
本文介绍了Javascript + Knockout.js内存泄漏-如何确保对象被销毁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Google的3快照方法来查找页面上的内存泄漏. 页面正在Knockout.js和Jquery Mobile UI上运行. 我已经缩小到内存泄漏div之一.在每个快照之间,我正在运行此脚本以清除内存泄漏对象.

I am using google's 3 Snapshot method to find memory leaks on the page. Page is running on Knockout.js and Jquery Mobile UI. I have already narrowed down to one of memory leaking divs. Between each snapshot I am running this script to clear the memory leaking object.

$('.paddedContentContainer').children().each(function(index,item){
console.log(item); 
$(item).off(); 
ko.cleanNode($(item)); 
$(item).remove();
item = null;
});

.paddedContentContainer是泄漏对象的父级.
现在有趣的部分是,我仍然可以在保留的对象树中看到对象.在下面的屏幕快照中,您可以看到我正在过滤快照3和快照1和2中保留的对象,通过文本可以看到,控制台中的$0在屏幕上的同一对象中(事件销售订单和.. ..).

.paddedContentContainer is parent of leaking object.
Now the interesting part is that I can still see object in retained object tree. In screenshot below you can see I am filtering on objects in Snapshot 3 that are retained from Snapshots 1 and 2, and by text it's visible that $0 from console in same object that is on the screen (Event Sales Order & ....).

我假设.off();.remove();不足以收集对象.
如何彻底销毁对该对象的所有引用?

I am assuming that .off(); and .remove(); isin't enough for object to be collected.
How to destroy all references to this object for good?

推荐答案

为仅对knockout.js参考删除感到满意的用户添加答案. 这样就从对象树中删除了<div>.

Adding an answer for those who will be satisfied with knockout.js reference removal only. This removed <div> at hand from object tree.

 $('.paddedContentContainer').children().each(function(index,item){
     $(item).off(); 
     ko.removeNode(item);
     $(item).remove();
     item = null;
 });

这篇关于Javascript + Knockout.js内存泄漏-如何确保对象被销毁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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