角+摩卡内存泄漏 [英] Angular + Mocha memory leak

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

问题描述

我正在测试的整套时收到以下错误:

I'm getting the following error when running the entire suite of tests:

2000毫秒的超时超标。确保完成()回调被调用的测试。

经过一番调查,我发现是内存泄漏问题。纵观一些堆纹快照,对象似乎仍然被引用,并没有得到garbaged收集。

After some investigation, I found out to be a memory leak issue. Looking at some heap profiling snapshot, objects still seem to be referenced and not getting garbaged collected.

任何人都知道,将prevent它发生的解决方案?有一些选项,比如通过我的1000ish规范中的每一个打算,加入 afterEach 做了一些清理,但这似乎是一个大量的工作。

Anyone know a solution that would prevent it from happening? There's some options such as going through each one of my 1000ish specs and adding afterEach to do some clean up, but that seems like a lot of work.

下面是一个如何我大部分的测试看起来像

Here's a sample layout of how most of my tests look like

describe('MyClassCtrl', function() {

  var $httpBackend, $rootScope, ctrl;
  ctrl = $rootScope = $httpBackend = null;

  beforeEach(function() {
    module('myApp');
    inject(function($controller, $rootScope, _$httpBackend_, $stateParams) {
      var $scope;
      $stateParams.id = 1;
      $httpBackend = _$httpBackend_;
      $scope = $rootScope.$new();
      ctrl = $controller('MyClassCtrl', {
        $scope: $scope
      });
    });
  });

  describe('#_getMyList', function() {
    beforeEach(function() {
      $httpBackend.expectGET("/my/app/url").respond({
        my_list: [1, 2, 3]
      });
      ctrl._getMyList();
      $httpBackend.flush();
    });

    it('does this', function() {
      expect(ctrl.my_list).to.eql([1, 2, 3]);
    });
  });
});

下面是一些分析截屏:

Below are some profiling screenshots:

在这里输入的形象描述

在这里输入的形象描述

在这里输入的形象描述

更新

我能够通过简单的包装我的在一个循环中的一个触发内存泄漏。

I was able to trigger a memory leak by simply wrapping one of my it in a loop.

例如:

for (i = 0; i < 200; i++) {
  it('does this', function() {
    expect(ctrl.my_list).to.eql([1, 2, 3]);
  });
}

在我的测试中,我将所有对象的容器对象内,并在 afterEach (如该解决方案的这里),但没有运气。内存分配仍保持对Chrome的开发工具的时间轴增加。

In my tests, I set all objects within a container object and cleaned it up in an afterEach (like the solution here) but no luck. Memory allocation still keep increasing on Chrome's Dev Timeline tool.

谢谢!

推荐答案

我不明白你清理你的嘲笑HTTP后端。我不知道它是如何发挥作用的,但我希望你所有的请求和响应被存储在内存中。这将导致它通过测试逐步增加,测试。

I don't see you cleaning up your mocked http backend. I have no idea how it functions, but I would expect that all your requests and responses are being stored in memory. That would cause it to increase gradually, test by test.

如果这是使用兴农我希望你会在测试之前创建一个存根/间谍事后清理(或在沙箱中运行它)。

If this was using Sinon I would expect that you would create a stub/spy before the tests and clean it up afterwards (or run it in a sandbox).

尝试前和运行测试,并在差异比较快照找出哪些对象保持越来越多服用后堆快照。

Try taking a heap snapshot before and after running a test and diff the snapshots to find which objects keep increasing in numbers.

这篇关于角+摩卡内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆