本文介绍了如何减少/消除Angular应用程序中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在优化我的大型Angular App.我发现Google DevTools非常适合发现问题.当我刚刚开始学习DevTools时,我对内存泄漏感到非常困惑.

I am optimizing my large Angular App. As I found a that Google DevTools is very good to detect problems. As I just have started learning about DevTools, I am very confused about memory leaks.

当我在应用程序中来回移动时,配置文件堆快照的大小一次又一次地增加,因此我认为有些对象没有被GC清除,这就是为什么我的应用程序在一段时间后变慢的原因.如何解决这个问题.请帮忙.

When I move back and from to different pages in my app, Profile heap Snapshot size is increasing again and again so I think there are some object which is not being cleaned by GC and that's why my app is getting slow after sometime so how to solve this. Please help.

注意

这是我使用DevTools所了解的,如果我错了,请纠正我.欢迎其他建议.

This is what I understand using DevTools, please correct me if I am wrong. Other suggestions are welcome.

现在为止我使用过的东西

  1. AngularOnce 指令,用于在需要时减少监视.
  2. QuickList指令,将ng-repeat替换为 quick-ng-repeat .
  3. InView 指令,用于处理大型列表,因此我要删除不在视口中的DOM.
  4. ngInfiniteScroll 指令中的延迟加载方法.
  1. AngularOnce directive for reducing watch whenever required.
  2. QuickList directive to replace ng-repeat with quick-ng-repeat.
  3. InView Directive, to handle large list so I am removing DOM which is not in viewport.
  4. Lazy load approach from ngInfiniteScroll directive.

推荐答案

  1. 删除绑定以避免内存泄漏,请使用范围 $ destroy()方法.

  1. Remove bindings to avoid memory leaks, Use Scopes$destroy() Method.

注意:

在您的指令中,请保持取消jQuery事件绑定的实践.$ destory方法,可用于清除DOM绑定之前的元素已从DOM中删除.

In your Directive keep practice for unbinding the jQuery Event.$destory Method which can be used to clean up DOM bindings before anelement is removed from the DOM.

 $scope.$on("$destroy",function() {
    $( window ).off( "resize.Viewport" );
 });

  • 不要忘记取消$ destroy事件中的$ timeout计时器AngularJS

  • Don't Forget To Cancel $timeout Timers In Your $destroy Events InAngularJS

    $scope.$on("$destroy",function( event ) {
        $timeout.cancel( timer );
    });
    

  • 这篇关于如何减少/消除Angular应用程序中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-27 06:34