简体   繁体   中英

Node.js catch ENOMEM error thrown after spawn

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?

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 :

  1. sudo fallocate -l 4G /swapfile Create a 4 gigabyte swapfile
  2. sudo chmod 600 /swapfile Secure the swapfile by restricting access to root
  3. sudo mkswap /swapfile Mark the file as a swap space
  4. sudo swapon /swapfile Enable the swap
  5. echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab Persist swapfile over reboots (thanks for the tip, bman !)

如果您在 AWS Lambda 中遇到过这个问题,您应该考虑增加分配给函数的内存。

You can try changing the amount of memory node uses with this command: node ----max-old-space-size=1024 yourscript.js

--max-old-space-size=1024 will allocate 1 gig of memory.

By default node will use 512 mb of ram but depending on your platform you may need to allocate more or less so the garbage collection kicks in when you need it.

If your platform has less than 500 mb of ram available then try setting the memory usage lower to --max-old-space-size=256.

I've had the same problem and fixed with try / catch:

try {
  zbarimg = process.spawn('zbarimg', [photo, '-q']);
} catch (err) {
  console.log(err);
}
zbarimg.on('error', function(err) { ... });
zbarimg.on('close', function(code) { ... }); 

我通过禁用和重新启用我的节点服务器解决了这个问题。

This solved my problem :)

The issue with memory

free -m
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo “/swapfile none swap sw 0 0” | sudo tee -a /etc/fstab

You have to flush the outputs from the called process!

A python example looks like this:

import sys
...
sys.stdout.flush()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM