Javascript Node.js内存不足

Javascript Node.js内存不足,javascript,node.js,crash,out-of-memory,heap-memory,Javascript,Node.js,Crash,Out Of Memory,Heap Memory,今天我运行了文件系统索引脚本以刷新RAID文件索引,4小时后它崩溃,出现以下错误: [md5:] 241613/241627 97.5% [md5:] 241614/241627 97.5% [md5:] 241625/241627 98.1% Creating missing list... (79570 files missing) Creating new files list... (241627 new files) <--- Last few GCs ---&g

今天我运行了文件系统索引脚本以刷新RAID文件索引,4小时后它崩溃,出现以下错误:

[md5:]  241613/241627 97.5%  
[md5:]  241614/241627 97.5%  
[md5:]  241625/241627 98.1%
Creating missing list... (79570 files missing)
Creating new files list... (241627 new files)

<--- Last few GCs --->

11629672 ms: Mark-sweep 1174.6 (1426.5) -> 1172.4 (1418.3) MB, 659.9 / 0 ms [allocation failure] [GC in old space requested].
11630371 ms: Mark-sweep 1172.4 (1418.3) -> 1172.4 (1411.3) MB, 698.9 / 0 ms [allocation failure] [GC in old space requested].
11631105 ms: Mark-sweep 1172.4 (1411.3) -> 1172.4 (1389.3) MB, 733.5 / 0 ms [last resort gc].
11631778 ms: Mark-sweep 1172.4 (1389.3) -> 1172.4 (1368.3) MB, 673.6 / 0 ms [last resort gc].


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0x3d1d329c9e59 <JS Object>
1: SparseJoinWithSeparatorJS(aka SparseJoinWithSeparatorJS) [native array.js:~84] [pc=0x3629ef689ad0] (this=0x3d1d32904189 <undefined>,w=0x2b690ce91071 <JS Array[241627]>,L=241627,M=0x3d1d329b4a11 <JS Function ConvertToString (SharedFunctionInfo 0x3d1d3294ef79)>,N=0x7c953bf4d49 <String[4]\: ,\n  >)
2: Join(aka Join) [native array.js:143] [pc=0x3629ef616696] (this=0x3d1d32904189 <undefin...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: node::Abort() [/usr/bin/node]
 2: 0xe2c5fc [/usr/bin/node]
 3: v8::Utils::ReportApiFailure(char const*, char const*) [/usr/bin/node]
 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/usr/bin/node]
 5: v8::internal::Factory::NewRawTwoByteString(int, v8::internal::PretenureFlag) [/usr/bin/node]
 6: v8::internal::Runtime_SparseJoinWithSeparator(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/bin/node]
 7: 0x3629ef50961b
[md5:]241613/241627 97.5%
[md5:]241614/241627 97.5%
[md5:]241625/241627 98.1%
正在创建缺少的列表。。。(79570个文件丢失)
正在创建新文件列表。。。(241627个新文件)
11629672毫秒:标记扫描1174.6(1426.5)->1172.4(1418.3)MB,659.9/0毫秒[分配失败][请求旧空间中的GC]。
11630371毫秒:标记扫描1172.4(1418.3)->1172.4(1411.3)MB,698.9/0毫秒[分配失败][请求旧空间中的GC]。
11631105毫秒:标记扫描1172.4(1411.3)->1172.4(1389.3)MB,733.5/0毫秒[最后的gc]。
11631778毫秒:标记扫描1172.4(1389.3)->1172.4(1368.3)MB,673.6/0毫秒[最后的gc]。
==JS堆栈跟踪=========================================
安全上下文:0x3d1d329c9e59
1:SparseJoinWithSeparatorJS(又称SparseJoinWithSeparatorJS)[native array.js:~84][pc=0x3629ef689ad0](this=0x3d1d32904189,w=0x2b690ce91071,L=241627,M=0x3d1d329b4a11,N=0x7c953bf4d49)

2:Join(又称Join)[native array.js:143][pc=0x3629ef616696](this=0x3d1d32904189如果我没记错的话,V8中的内存使用量有一个严格的标准限制,大约为1.7 GB,如果您不手动增加它的话

在我们的一款产品中,我们在部署脚本中遵循了此解决方案:

 node --max-old-space-size=4096 yourFile.js

还有一个新的space命令,但正如我在这里读到的:新的space只收集新创建的短期数据,旧的space包含所有引用的数据结构,这在您的情况下应该是最好的选择。

如果我没记错的话,V8中的内存使用有一个严格的标准限制,大约为1.7GB,如果您这样做的话不要手动增加它

在我们的一款产品中,我们在部署脚本中遵循了此解决方案:

 node --max-old-space-size=4096 yourFile.js

还有一个新的space命令,但正如我在这里读到的:新的space只收集新创建的短期数据,而旧的space包含所有引用的数据结构,在您的情况下,这应该是最好的选择。

我在尝试使用VSCode调试时遇到了这个问题,所以我只想添加这是添加argu的方式调试设置中的默认设置

您可以将其添加到
launch.json
中配置的
runtimeArgs
属性中

见下面的例子

{
"version": "0.2.0",
"configurations": [{
        "type": "node",
        "request": "launch",
        "name": "Launch Program",
        "program": "${workspaceRoot}\\server.js"
    },
    {
        "type": "node",
        "request": "launch",
        "name": "Launch Training Script",
        "program": "${workspaceRoot}\\training-script.js",
        "runtimeArgs": [
            "--max-old-space-size=4096"
        ]
    }
]}

我在尝试使用VSCode进行调试时遇到了这个问题,因此我只想添加以下内容,即如何将参数添加到调试设置中

您可以将其添加到
launch.json
中配置的
runtimeArgs
属性中

见下面的例子

{
"version": "0.2.0",
"configurations": [{
        "type": "node",
        "request": "launch",
        "name": "Launch Program",
        "program": "${workspaceRoot}\\server.js"
    },
    {
        "type": "node",
        "request": "launch",
        "name": "Launch Training Script",
        "program": "${workspaceRoot}\\training-script.js",
        "runtimeArgs": [
            "--max-old-space-size=4096"
        ]
    }
]}

即使在设置了“最大旧空间大小”之后,我仍在努力解决这个问题

然后我意识到需要在karma脚本之前放置选项——最大旧空间大小

最好同时指定语法--max old space size和--max_old_space_size我的因果报应脚本:

node --max-old-space-size=8192 --optimize-for-size --max-executable-size=8192  --max_old_space_size=8192 --optimize_for_size --max_executable_size=8192 node_modules/karma/bin/karma start --single-run --max_new_space_size=8192   --prod --aot

参考

即使在设置了--max old space size之后,我仍在努力解决这个问题

然后我意识到需要在karma脚本之前放置选项——最大旧空间大小

最好同时指定语法--max old space size和--max_old_space_size我的因果报应脚本:

node --max-old-space-size=8192 --optimize-for-size --max-executable-size=8192  --max_old_space_size=8192 --optimize_for_size --max_executable_size=8192 node_modules/karma/bin/karma start --single-run --max_new_space_size=8192   --prod --aot

参考

fwiw,使用类似的东西查找和修复内存占用空间可能会有所帮助。

fwiw,使用类似的东西查找和修复内存占用空间可能会有所帮助。

将节点升级到最新版本。我在节点6.6上出现此错误并升级到8.9.4,问题消失。

将节点升级到最新版本。我在节点上6.6出现此错误并升级到8.9.4,问题就消失了。

为了防止在使用产生大量日志记录的nodejs应用程序时出现此问题,一位同事通过管道传输标准输出解决了此问题为了防止人们在使用产生大量日志记录的nodejs应用程序时遇到此问题,一位同事通过将标准输出管道连接到文件解决了此问题。

我在执行AOT angular build时遇到类似问题。以下命令对我有帮助

npm install -g increase-memory-limit
increase-memory-limit

来源:

我在进行AOT角度构建时遇到了类似的问题。以下命令对我有所帮助

npm install -g increase-memory-limit
increase-memory-limit

来源:

以防任何人在无法直接设置节点属性的环境中遇到此问题(在我的示例中是生成工具):


如果无法在命令行上传递节点选项,则可以使用环境变量设置节点选项。

以防任何人在无法直接设置节点属性的环境中遇到此选项(在我的示例中是生成工具):

如果无法在命令行上传递节点选项,则可以使用环境变量设置这些选项

修复此问题的步骤(在Windows中)-

  • 打开命令提示符并键入
    %appdata%
    按enter键
  • 导航到
    %appdata%
    >npm文件夹
  • 在您喜爱的编辑器中打开或编辑
    ng.cmd
  • --max\u old\u space\u size=8192
    添加到IF和ELSE块
  • 更改后,
    node.cmd
    文件如下所示:

    @IF EXIST "%~dp0\node.exe" (
      "%~dp0\node.exe" "--max_old_space_size=8192" "%~dp0\node_modules\@angular\cli\bin\ng" %*
    ) ELSE (
      @SETLOCAL
      @SET PATHEXT=%PATHEXT:;.JS;=;%
      node "--max_old_space_size=8192" "%~dp0\node_modules\@angular\cli\bin\ng" %*
    )
    
    修复此问题的步骤(在Windows中)-

  • 打开命令提示符并键入
    %appdata%
    按enter键
  • 导航到
    %appdata%
    >npm文件夹
  • 在您喜爱的编辑器中打开或编辑
    ng.cmd
  • --max\u old\u space\u size=8192
    添加到IF和ELSE块
  • 更改后,
    node.cmd
    文件如下所示:

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

    如果您想在全局范围内增加节点的内存使用,而不仅仅是单个脚本,您可以导出环境变量,如下所示:
    export NODE\u OPTIONS=--max\u old\u space\u size=4096

    这样,在运行构建时就不需要处理文件,如
    npm run build

    如果您希望全局增加节点的内存使用率,不仅可以使用单个脚本,还可以导出环境变量,如下所示:
    export NODE\u OPTIONS=--max\u old\u space\u size=4096

    然后你会
    NODE_OPTIONS= --max-old-space-size=8192 .
    
    variable name = NODE_OPTIONS
    variable value = --max-old-space-size=4096
    
    "build-prod": "node --max_old_space_size=5120 ./node_modules/@angular/cli/bin/ng build --prod --base-href /"
    
    terminate called after throwing an instance of 'std::bad_alloc'
      what():  std::bad_alloc
    Aborted (core dumped)
    
    sysctl vm.max_map_count
    
    sysctl -w vm.max_map_count=655300
    
    vm.max_map_count=655300
    
    strace node --max-old-space-size=128000 my_memory_consuming_process.js
    
    node --expose-gc --max-old-space-size=8192 node_modules/react-scripts/scripts/build.js
    
     $env:NODE_OPTIONS="--max-old-space-size=8192"
    
    "scripts": {
      "build-prod": "node --max_old_space_size=5048 ./node_modules/@angular/cli/bin/ng build --prod",
    },
    
    npm run build-prod
    
    {
       "scripts":{
          "server":"node --max-old-space-size={size-value} server/index.js"
       }
    }
    
    data => res.send(data) // this will not block your thread, apply everywhere where it's needed
    
    `error => { next(err) } // here err is undefined`
    
     `err => {next(error) } // here error is undefined`
    
     `app.get(API , (re,res) =>{
         error => next(error) // here next is not defined
      })`
    
      if(successCB) console.log("success") successCB(response.data) // here it's wrong statement, because on success you are just logging and then `successCB` sending  outside the if block which return in failure case also.