javascript - 当内存可用时,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 方法
解决方案
据我所知,node.js 有一个默认的内存限制。32 位系统上的 1G 和 64 位系统上的 1.7G 之类的东西。--max_old_space_size=<size>
但是,幸运的是,您可以通过node.js 命令行中的 issue 选项来增加它。以 Mb 为单位的价值<size>
。
推荐阅读
- reactjs - 如何正确访问组件内的道具
- node.js - 根据条件停止和开始执行 cron-job-manager (node-cron)
- javascript - 达到 min-max 时,在递增/递减按钮上添加/删除“禁用”属性
- python - Seaborn X 轴分类数据
- java - 如何使 Firebase ".info/connected" 和 .onDisconnect() 相互准确?
- asp.net - 当 web.config 可见属性设置为 false 时,Visual Studio 不运行
- python - Python Multiprocessing - 我们可以将(itertools.islice)可迭代直接传递给pool.imap而不转换为列表吗?
- python - Django中的循环对象
- python - Pycharm - 设置用户名和密码登录脚本
- python-3.7 - 用户属性更新时如何触发事件?