jjzjj

javascript - angularjs 1.5 : How to identify what is getting leaked and fix the leak?

coder 2024-07-20 原文

在 chrome latest 和其他浏览器中测试。此页面启动一个 timer() 每 60 秒刷新一次。在 init() 和每个 refresh() 上,它从服务器获取数据并在页面中显示相同的数据。我们看到它每次刷新都会泄漏大量 MB。

  1. 现在,我如何识别被泄露的特定对象和/或 DOM 节点

  2. 一旦我从 #1 中识别出对象/节点,我该如何着手修复漏洞?

是否有任何书籍、好的教程可以涵盖 Angularjs 1.5 的上述内容?

最佳答案

您可能找到了 https://developers.google.com/web/tools/chrome-devtools/memory-problems/http://www.dwmkerr.com/fixing-memory-leaks-in-angularjs-applications/因为那里没有更详细的资源。

A DOM node can only be garbage collected when there are no references to it from either the page's DOM tree or JavaScript code. A node is said to be "detached" when it's removed from the DOM tree but some JavaScript still references it. Detached DOM nodes are a common cause of memory leaks.

  1. 如果您没有持有计时器的引用,而是在每次刷新时创建一个新计时器 - 泄漏,可通过重用 $timeout

  2. 解决
  3. Checkout - CTRL + F $scope 由闭包上下文保留。 在第二个提供的链接上。那里解释的用例与您所拥有的用例非常相似。文章进一步:

We can open the function and examine it for issues. There's an $http.get which has as closure which uses $scope, but alarmingly there is an $interval registered to run every 10 seconds, which is never deregistered. The interval callback uses another $http.get, with a closure that uses $scope. This is the problem.

如果以上都不适用,那么这里是 AngularJS 中以 memory leak 作为关键字的未决问题列表:

https://github.com/angular/angular.js/issues?utf8=%E2%9C%93&q=is%3Aopen%20memory%20leak

关于javascript - angularjs 1.5 : How to identify what is getting leaked and fix the leak?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45628419/

有关javascript - angularjs 1.5 : How to identify what is getting leaked and fix the leak?的更多相关文章

随机推荐