KnockoutJS内存泄漏 [英] KnockoutJS Memory Leak

查看:57
本文介绍了KnockoutJS内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相当确定我使用KO 2.0版时发生内存泄漏.我有一个可观察的数组,其中填充了AJAX调用的结果.此集合与每个DIV容器的数据绑定在一起.数组中的每个对象都有一个绑定到复选框的单个可观察值.我已经使用Chrome浏览了该堆,得出的结论如下:

I'm fairly certain I'm having memory leaks using KO version 2.0. I have an observable array that is populated with the result of an AJAX call. This collection is data-bound with a for each to a DIV container. Each object in the array has one single observable value that is bound to a checkbox. I've examined the heap using Chrome and my conclusion is the following:

如果AJAX调用返回3个元素,则它们将在DOM上正确呈现.如果此时我对堆进行快照,则其中有三个SearchResult对象.如果我再次触发AJAX调用并返回5个元素,则所有5个元素都将正确呈现给DOM.但是,如果我在Chrome中对堆进行快照并进行比较,则列出了8个仍在堆中的元素,所有这些元素都被列为已添加",而没有一个元素被列为已删除". DOM显示始终是正确的,但是由于旧的搜索结果从未被释放,因此内存的使用一直在不断攀升.

If the AJAX call returns 3 elements, they are rendered properly on the DOM. If I take a snapshot of the heap at this point, there are three SearchResult objects in there. If I trigger the AJAX call again and it returns 5 elements, all 5 are correctly rendered to the DOM. However, if I take a snapshot of the heap in Chrome, and compare them, there are 8 elements listed as still being on the heap, all of them listed as being "added" and none are listed as "deleted". The DOM display is always correct, but the memory use just keeps climbing and climbing because the old search results are never deallocated.

有人可以帮助我,也可以给我一些诊断内存泄漏的提示吗?

Can anyone help me or give me pointers for diagnosing the memory leak?

更新

我创建了一个 jsFiddle ,以显示我在做什么.我已经精简了所有内容,但是其核心功能和在本地计算机上运行时仍然可以复制内存泄漏.显然,该代码将无法发布,因为它需要进入我的本地服务器才能运行搜索.

I've created a jsFiddle to show the gist of what I'm doing. I've striped down EVERYTHING but the core functionality and I can still duplicate the memory leak when running on my local machine. Obviously the code won't work as it's posted because it needs to hit my local server to run the search.

更新2

我下载了最新的2.1.0.0 Beta版本,该漏洞消失了.我不太喜欢使用Beta版本的东西或经典的只是升级到新版本"修复程序.我仍然很想知道发生了什么变化或我做错了哪些会导致泄漏.

I pulled the newest 2.1.0.0 Beta version and the leak disappeared. I'm not a huge fan of using beta version of things or of the classic "just upgrade to the new version" fix. I am still very interested in knowing what changed or what I was doing wrong that was creating the leak.

推荐答案

您没有做错任何事情,好像ko.cleanNode忽略了foreach绑定并且没有正确处理更新后的.

You're not doing anything wrong, it looks like ko.cleanNode was ignoring foreach bindings and not properly disposing of the outdated objects within the updated observableArray.

https://github.com/SteveSanderson/knockout/issues/271

此问题已在2.1.0beta版中修复

This has been fixed in 2.1.0beta

这篇关于KnockoutJS内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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