我正在优化我的大型 Angular App
.正如我发现的 Google DevTools
很好的发现问题。因为我刚刚开始了解 DevTools
,我对内存泄漏感到很困惑。
当我在我的应用程序中来回移动到不同页面时,配置文件堆快照大小一次又一次地增加,所以我认为有些对象没有被 GC 清理,这就是为什么我的应用程序在一段时间后变慢所以如何解决这。请帮忙。
备注
这是我使用 DevTools 所理解的,如果我错了,请纠正我。欢迎提出其他建议。
到现在为止我用过的
最佳答案
$destroy()方法。
注:
The most likely culprit of memory leak in Angular is JQuery used in your directives. If you attach an event-listener in your directive using a JQuery plugin, the latter would keep a reference to your DOM even after Angular deletes its own reference to the DOM, which means it would never be garbage-collected by the browser, which in turn means “Detached DOM tree” in your memory
在您的指令中,请继续练习解除 jQuery 事件的绑定(bind)。
$destory 方法,可用于在执行之前清理 DOM 绑定(bind)
元素从 DOM 中移除。
$scope.$on("$destroy",function() {
$( window ).off( "resize.Viewport" );
});
AngularJS
$scope.$on("$destroy",function( event ) {
$timeout.cancel( timer );
});
关于angularjs - 如何减少/消除 Angular 应用程序中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22397484/