【问题标题】:ng test JavaScript heap out of memory in angular due to large mock data由于大型模拟数据,ng test JavaScript heap out of memory in angular
【发布时间】:2017-11-14 14:55:32
【问题描述】:

我在我的测试用例中使用 Angular 4 进行测试,我需要大量数据。 我动态地需要我的规范文件中需要的 json。 这些文件大约 4 到 5 MB。

这里有一些参考...

  it('Multiple JSON:', () => {
    rdocList.forEach(element => {
      if (element.rdoc_id) {
        service.rdoc = require('../mock/' +
          element.rdoc_id +
          '.json');
        service.rdoc = new RDoc(service.rdoc);
        const storage = service.rdoc.descendants.filter({
          class_id: STORAGE_CLASS_IDS.ID
        });
        const clusterNode = storage[0].descendants.filter({
          class_id: STORAGE_CLASS_IDS.CLUSTERID
        });
        component.id = clusterNode[0].id;
        fixture.detectChanges();
      }
    });
  });

我已经清除了每个之后的 json..

  afterEach(() => {
    service.rdoc = null;
    service = null;
  });

我遇到了这个问题

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

【问题讨论】:

    标签: angular testing karma-jasmine


    【解决方案1】:

    对我来说,这不是简单的ng test,而是:
    node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng test
    其中8192 是您要分配的dissered 内存

    【讨论】:

      【解决方案2】:

      您也可以先通过运行export NODE_OPTIONS=--max_old_space_size=8192,然后运行ng test 命令来全局增加节点的内存使用率。

      【讨论】:

        【解决方案3】:

        移动到此路径中的 npm 位置 - C:\Users*UserName*\AppData\Roaming\npm

        尝试将 ng.cmd 更改为:

        @IF EXIST "%~dp0\node.exe" (
           "%~dp0\node.exe" --max_old_space_size=8192 "%~dp0\..\@angular\cli\bin\ng" %*
        ) ELSE (
           @SETLOCAL
           @SET PATHEXT=%PATHEXT:;.JS;=;%
           node --max_old_space_size=8192 "%~dp0\..\@angular\cli\bin\ng" %*
        )
        

        ngc.cmd 更改为:

        @IF EXIST "%~dp0\node.exe" (
               "%~dp0\node.exe" --max_old_space_size=8192 "%~dp0\..\@angular\compiler-cli\src\main.js" %*
         ) ELSE (
               @SETLOCAL
               @SET PATHEXT=%PATHEXT:;.JS;=;%
               node --max_old_space_size=8192 "%~dp0\..\@angular\compiler-cli\src\main.js" %*
         )
        

        【讨论】:

        • 我已经完成了它仍然失败我的系统有 32 GB RAM。我的问题是如何在运行测试时管理 GC
        猜你喜欢
        • 2018-08-01
        • 2017-05-21
        • 1970-01-01
        • 1970-01-01
        • 2019-06-18
        • 2019-10-18
        • 1970-01-01
        • 2021-09-02
        • 1970-01-01
        相关资源
        最近更新 更多