JavaScript 内存泄漏。由于函数变量,对象未被删除

标签 javascript typescript google-chrome-devtools

我创建了一个对象 this.bookView = new BookView();

当我执行 this.bookView=null 时,垃圾收集器没有删除该对象。

Google Dev Tool 配置文件快照保留器窗口显示以下连接(这表明某些内容引用了该对象)。

enter image description here

除了() 中的上下文之外,所有行都是“预览不可用”

enter image description here

enter image description here

问题是 BookViewaddSheet 函数变量,该变量尚未在任何地方使用,但通过 _this 保存对自身的引用.

我尝试过的:

1) this.bookView.addSheet = null;(没有帮助)

2) 删除 this.bookView.addSheet; (没有帮助)

我的问题:

1) 如何从Window中删除此连接并使bookView被垃圾收集?

2) 为什么addSheet变量是从Window对象链接的?

3) retainers 窗口上的转换描述符是什么?

4)我还可以使用哪些其他工具来查找有关此类内存泄漏的更多详细信息。

附注bookView 对象是在 Durandal 页面内创建的。 this.bookView 基本上是一个 page1.bookView。页面已正确删除。在 deactivate (页面删除)功能期间,我调用 this.bookView = null;

最佳答案

如果您没有在 function 中实例化 BookViewthis 将是您的 window 对象,这是全局性的。您已将其全局附加到一个对象,该对象将一直存在,直到您关闭浏览器或选项卡为止。您可以尝试通过删除 window['bookView']

来摆脱 BookView

编辑:在 Durandal 页面中停用回调,运行 this 并删除其键。

for(var key in this)
   delete this[key];

关于JavaScript 内存泄漏。由于函数变量,对象未被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41968885/

相关文章:

javascript - vscode 和 jsdocs - 如何将 TS 接口(interface)或类型用于 javascript 函数

javascript - 正文解析器不从表单读取数据

javascript - Ajax 不工作 - 不断收到 $.ajax 不是函数错误

javascript - Xcode Javascript JSON 解析

css - 如何设置元素的:hover state in Chrome dev tools and edit child's style

google-chrome - Chrome 开发工具 "Find"范围 : Search through Resources (scripts, 样式表等)

google-chrome - Chrome DevTools 切换评论快捷方式不起作用

javascript - 为什么 Bootstrap 崩溃会自动折叠(收缩)元素?

typescript - 混合联合类型、泛型和条件类型导致意外 "Type is not assignable to type"错误

typescript - 类型 '{}' 不可分配给类型 'T'