KnockoutJS 内存泄漏 [英] KnockoutJS Memory Leak

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

问题描述

我很确定我在使用 KO 2.0 版时遇到了内存泄漏.我有一个 observable 数组,其中填充了 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 版本,泄漏消失了.我不喜欢使用测试版的东西或经典的只需升级到新版本"修复程序.我仍然很想知道导致泄漏的更改或我做错了什么.

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 绑定和未正确处理更新后的 observableArray 中的过时对象.

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天全站免登陆