gpt4 book ai didi

javascript - 多次尝试清理后,Backbone 仍然在泄漏内存

转载 作者:行者123 更新时间:2023-11-29 19:50:02 26 4
gpt4 key购买 nike

我在解决主干内存泄漏方面遇到问题,到目前为止,当 View 从 DOM 中删除时,我尝试使用 jquery CleanData 清理 View 。

每次删除 View (即使通过 jQuery .html())时,它都会触发 dispose() 方法,该方法理论上应该杀死所有阻止 View 被收集的引用。不幸的是,该应用程序只会增加内存

下面的代码,

Backbone.View.prototype.dispose = function(){
// unbind all events, so we don't have references to child views
this.unbind();
this.off();
// if we have a collection - unbind all events bound to this context
this.collection && this.collection.off(null, null, this);
// do the same with a model
this.model && this.model.off(null, null, this);
delete this;
};

清理数据:

$.cleanData = function( elems ) {
$.each( elems, function( i, elem ) {
if ( elem ) {
$(elem).trigger("dispose");
}
});
oldClean(elems);
};

代码有效,处理被命中(我在 View 中添加了事件)但是当我更改页面时它永远不会被收集。

(关于处置事件..)我没有在所有 View 上明确调用 remove。应用程序容器被清空,jQuery 执行 cleanData。我添加了一个事件处理并在 cleandata 中触发了该函数

最佳答案

我认为问题是delete this 没有按照您的预期进行。这取决于您如何启动 View 。您是否将 View 分配给任何变量或在超出页面更改点的范围内启动它?

还有一个函数remove()在主干 View 上

关于 JavaScript delete 的更多信息 http://perfectionkills.com/understanding-delete/https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

关于javascript - 多次尝试清理后,Backbone 仍然在泄漏内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18049087/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com