首页 > 解决方案 > 当内存可用时,NodeJS exec 抛出 ENOMEM

问题描述

我正在运行 pdfinfo 命令来获取有关大小为 100KB 的 pdf 的信息。相关代码是

    const pdfInfo = execute(`pdfinfo '${input}' | grep Pages`)
      .then(stdout => {
        var pdfLength = 0;
        pdfLength = stdout.match(/Pages:\s+(\d+)/)[1];
        return pdfLength;
    })
    function execute(command) {
      return new Promise(function (res, rej) {
        exec(command, function (error, stdout, stderr) {
          if (error || stderr) {
            rej(error || stderr);
          } else {
            res(stdout);
          }
        });
      });
    }

处理几个文件后,内存使用量不断增加

 PM2 details of the process
 Mem: 431 MB    CPU:  0 %  

如果我看到服务器上可用的内存,那就是

/home/core-server # free -m
             total       used       free     shared    buffers     cached
Mem:          2000       1341        659         20         21        173
-/+ buffers/cache:       1145        854
Swap:            0          0          0

内存可用,但我仍然收到错误

2019-01-10T00:46:42.266Z Error: spawn ENOMEM
    at ChildProcess.spawn (internal/child_process.js:358:11)
    at spawn (child_process.js:533:9)
    at Object.execFile (child_process.js:216:15)
    at exec (child_process.js:147:18)
    at /home/core-server/helper/ppt2svg.js:42:5
    at new Promise (<anonymous>)
    at execute (/home/core-server/helper/ppt2svg.js:41:10)
    at pdf2svg (/home/core-server/helper/ppt2svg.js:53:18)
    at ppt2svg (/home/core-server/helper/ppt2svg.js:93:12)
    at uploadPresentation.single.err (/home/core-server/api/sessions/index.js:174:25)

任何人都可以向我提供任何想法,说明为什么会发生这种情况?

编辑:顺便说一句,服务器上的其他一切工作正常,但它是有这个问题的 exec 方法

标签: javascriptnode.js

解决方案


据我所知,node.js 有一个默认的内存限制。32 位系统上的 1G 和 64 位系统上的 1.7G 之类的东西。--max_old_space_size=<size>但是,幸运的是,您可以通过node.js 命令行中的 issue 选项来增加它。以 Mb 为单位的价值<size>


推荐阅读