Node.js catch ENOMEM error thrown after caviar

My Node.js script crashes due to an ENOMEM (Out of memory) errnoException failure when using spawn .

Mistake:

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 already use listeners for error and exit events, but they do not fire in the event of this error.

My code is:

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

Full source code is available .

Is there anything I can do to prevent the script from crashing? How to catch the thrown ENOMEM error?

Thank!

+64
javascript try-catch error-handling spawn
04 Oct '14 at 14:13
source share
5 answers

I had the same problem, and as it turned out, there was no swap space on my system. Verify this by running the free -m command:

 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 line, we see that we have a shared memory with 0 bytes. Not good. Node can get quite hungry memory, and if free paging space is not available when memory runs out, errors will occur.

The method for adding a swap file depends on operating systems and distributions, but if you use Ubuntu like me, you can follow these instructions when adding a swap file :

  • sudo fallocate -l 4G /swapfile Create a 4 gigabyte swap file
  • sudo chmod 600 /swapfile Protect the sudo chmod 600 /swapfile file by restricting root access
  • sudo mkswap /swapfile Mark file as swap space
  • sudo swapon /swapfile Enable sharing
  • 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 swap file through reboots (thanks for the help, bman !)
+154
Sep 07 '15 at 18:58
source share

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

- max-old-space-size = 1024 will allocate 1 gigabyte 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 that garbage collection works when you need it.

If your platform has less than 500 mb of available plunger, try setting a lower memory usage of -max-old-space-size = 256.

+1
Jan 9
source share

I 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) { ... }); 
0
Aug 03 '16 at 9:09 on
source share

If you have ever encountered this problem in AWS Lambda, you should consider increasing the memory allocated for the function.

0
Apr 14 '19 at 22:41
source share

You must clear the exits from the called process!

An example python looks like this:

 import sys ... sys.stdout.flush() 
-3
Nov 13 '17 at 7:02
source share



All Articles