问题描述
我的Node.js脚本因使用而抛出的ENOMEM(内存不足)errnoException而崩溃。 p>
错误:
child_process.js:935
throw errnoException(process._errno,'spawn');
^
错误:在errnoException(child_process.js:988:11)中生成ENOMEM
$ ChildBasic.spawn中的
(child_process.js:935:11)
在Object.exports.spawn(child_process.js:723:9)
在module.exports([...] / node_modules / zbarimg / index.js:19:23)
我已经在错误
和 exit
事件,但是如果出现此错误,则不会被触发。
我的代码:
zbarimg = process.spawn('zbarimg',[photo,'-q]])
zbarimg.on('error',function(err){...});
zbarimg.on('close',function(code){...});
完整的源代码。
有什么可以做的,以防止脚本崩溃?谢谢!
>我有同样的问题,事实证明,我的系统已经没有交换空间。通过运行命令 free -m
来检查是否如此:
vagrant @ vagrant-ubuntu-trusty-64:〜$ free -m
总共使用免费共享缓冲区缓存
内存:2002 233 1769 0 24 91
- / + buffers / cache:116 1885
交换:0 0 0
查看底行我们可以看到我们有总共0字节交换内存。不好。节点可以让内存变得饥饿,如果内存不足时没有可用的交换空间,则会发生错误。
添加交换文件的方法因操作系统而异和发行版,但是如果你像我一样运行Ubuntu,你可以按照这些:
-
sudo fallocate -l 4G / swapfile
创建一个4 GB的交换文件 -
sudo chmod 600 / swapfile
通过限制访问来保护交换文件到根 -
sudo mkswap / swapfile
将文件标记为交换空间 -
sudo swapon / swapfile
启用交换 -
echo/ swapfile none swap sw 0 0 sudo tee -a / etc / fstab
在重新启动时持久交换文件(谢谢提示, !)
My Node.js script crashes because of a thrown ENOMEM (Out of memory) errnoException when using spawn.
The error:
child_process.js:935
throw errnoException(process._errno, 'spawn');
^
Error: spawn ENOMEM
at errnoException (child_process.js:988:11)
at ChildProcess.spawn (child_process.js:935:11)
at Object.exports.spawn (child_process.js:723:9)
at module.exports ([...]/node_modules/zbarimg/index.js:19:23)
I'm already using listeners for the error
and exit
event, but non of them getting fired in case of this error.
My code:
zbarimg = process.spawn('zbarimg', [photo, '-q']);
zbarimg.on('error', function(err) { ... });
zbarimg.on('close', function(code) { ... });
Full source code available.
Is there anything I can do to prevent the script from crashing? How do I catch the thrown ENOMEM error?
Thanks!
I had the same problem and as it turned out, my system had no swap space enabled. Check if this is the case by running the command free -m
:
vagrant@vagrant-ubuntu-trusty-64:~$ free -m
total used free shared buffers cached
Mem: 2002 233 1769 0 24 91
-/+ buffers/cache: 116 1885
Swap: 0 0 0
Looking at the bottom row we can see we have a total of 0 bytes swap memory. Not good. Node can get pretty memory hungry and if no swap space is available when memory runs out, errors are bound to happen.
The method for adding a swap file varies between operating systems and distributions, but if you're running Ubuntu like me you can follow these instructions on adding a swap file:
sudo fallocate -l 4G /swapfile
Create a 4 gigabyte swapfilesudo chmod 600 /swapfile
Secure the swapfile by restricting access to rootsudo mkswap /swapfile
Mark the file as a swap spacesudo swapon /swapfile
Enable the swapecho "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
Persist swapfile over reboots (thanks for the tip, bman!)
这篇关于Node.js捕获生成后抛出的ENOMEM错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!