3月 132020
 

近日PM2下的nodejs应用出现下面错误:

PM2 error: Trace: { Error: spawn ENOMEM
at exports._errnoException (util.js:1020:11)
at ChildProcess.spawn (internal/child_process.js:328:11)
at exports.spawn (child_process.js:370:9)
at /usr/lib/node_modules/pm2/lib/God/ForkMode.js:112:20
at /usr/lib/node_modules/pm2/node_modules/async/internal/once.js:12:16
at next (/usr/lib/node_modules/pm2/node_modules/async/waterfall.js:21:29)
at /usr/lib/node_modules/pm2/node_modules/async/internal/onlyOnce.js:12:16
at WriteStream.<anonymous> (/usr/lib/node_modules/pm2/lib/Utility.js:181:13)
at emitOne (events.js:96:13)
at WriteStream.emit (events.js:188:7) code: ‘ENOMEM’, errno: ‘ENOMEM’, syscall: ‘spawn’ }
at Object.God.logAndGenerateError (/usr/lib/node_modules/pm2/lib/God/Methods.js:34:15)
at /usr/lib/node_modules/pm2/lib/God/ForkMode.js:114:13
at /usr/lib/node_modules/pm2/node_modules/async/internal/once.js:12:16
at next (/usr/lib/node_modules/pm2/node_modules/async/waterfall.js:21:29)
at /usr/lib/node_modules/pm2/node_modules/async/internal/onlyOnce.js:12:16
at WriteStream.<anonymous> (/usr/lib/node_modules/pm2/lib/Utility.js:181:13)
at emitOne (events.js:96:13)
at WriteStream.emit (events.js:188:7)
at WriteStream.<anonymous> (fs.js:2144:10)
at FSReqWrap.oncomplete (fs.js:123:15)

我在程序里做了process.on(“uncaughtException”)捕获处理,但碰到ENOMEM,也没有进入这个处理使用free -m, 发现这台机器no swap space enabled. 没有设置交换分区,可行方案:

设置交换分区,操作如下:

# dd if=/dev/zero of=/var/cacheswap bs=1M count=8192
# mkswap /var/cacheswap
# swapon /var/cacheswap

bs=1M count=8192 意味着它会创建8GB的Swap文件,您可以根据需要改变。 创建Swap之后,在我们的系统已通过运行“free -m”命令,看到Swap已启用。 为了使它能够在系统启动时自动加载,只需编辑/etc/fstab文件,并添加以下在文件的末尾条目。
/var/cacheswap swap swap defaults 0 0

 

问题虽然可以解决,但也有可能是应用程序代码存在着内存泄漏(memory leak),这个还需继续排查

 

Sorry, the comment form is closed at this time.