105

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
tobi
  • 1,924
  • 2
  • 20
  • 25

6 Answers6

244

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 Persist swapfile over reboots (thanks for the tip, bman!)
Kaivosukeltaja
  • 15,541
  • 4
  • 40
  • 70
  • 21
    Just a note for anyone in future who is reading this answer. Swapfile is not persistent on reboots. To make it persistent you need to edit /etc/fstab file and add a line at the end: /swapfile none swap sw 0 0 – bman Sep 05 '16 at 09:24
  • Just giving my stupid VM 2 more gigs of ram resolved my above issue. – Thomson Comer Jan 04 '17 at 23:36
  • 4
    Is this a good idea on a production server? My understanding is that when the OS starts using swap memory, performance can degrade sharply, so it's better to size your server with enough RAM to handle the applications' needs, and aggressively hunt down memory leaks. – josh Feb 28 '17 at 23:23
  • 4
    @josh, when RAM runs out one of two things will happen - either memory will get paged to a swapfile or any requests for additional memory will fail with unexpected results. Yes, performance may degrade when a swapfile is used but I'll take that any day over the other option, *especially* in production. – Kaivosukeltaja Mar 01 '17 at 11:31
  • I didn't do double the memory and need to resize? How do I do this? – Jack Feb 10 '18 at 14:37
  • I encountered this issue when I was trying to run angular project with `ng serve` command and `--prod` flag on **rancher**. In testing environment the deployment was running without issues because it ran without `--prod` flag. As you mentioned we decided to increase memory limit in rancher service form **1024** for **500MB**. This did a trick, service was successfully deployed. Next I have to migrate to some real production ready web server, and build app infront. – RenatoIvancic Jun 05 '18 at 14:54
  • 1
    After much searching about this answer is all I needed (and taught me about swap memory). Would highly suggest bumping this up as the answer – GroomedGorilla Sep 04 '18 at 14:13
  • This might be a dumb question since I'm new to this but is there anyway to reset the memory or clear cache from the js file running? I had a file running and after several runs it gave me this error. @Kaivosukeltaja – FabricioG Nov 27 '18 at 02:38
  • Indeed, this was the solution to my problem to deploy a dockerized meteor app, at docker-compose up. It works like a charm, Thanks ! – Moff452 Jun 16 '19 at 15:39
10

If you ever run into this problem in AWS Lambda, you should consider increasing the memory allocated to the function.

James Shapiro
  • 4,805
  • 3
  • 31
  • 46
4

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.

Deemoe
  • 931
  • 10
  • 12
3

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
Ezequiel Fernandez
  • 954
  • 11
  • 18
0

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) { ... }); 
Nodarius
  • 366
  • 4
  • 15
-3

I fixed the issue by just disabling and re-enabling my Node Server.

Carlos G
  • 479
  • 1
  • 8
  • 19