Fix for Nodejs – Cannot allocate memory and Process out of memory issues

Prabu D

1 min read

When you face the error Uncaught Error: ExecJS::RuntimeError: FATAL ERROR: Evacuation Allocation failed - process out of memory and rails assets pipeline Cannot allocate memory - nodejs while deploying and running a Ruby on Rails, you can fix it by clearing and increasing the SWAP file.

It happens because of the node process consumed all the available memory.
Screen Shot 2015-10-16 at 10.52.00 AM
You can fix it by the following option with out restarting production server.
Option 1: Clear Ram Cache (clear ram-cache only when required)
Linux provides a way to flush or clear ram cache. You can use the following command to clear the memory when the process is eating away your memory.
To free pagecache, dentries and inodes
[source]
sync; echo 3 > /proc/sys/vm/drop_caches
[/source]
Option 2: Create and Enable a Swap File
To prevent out of memory errors in server, add some swap space, it helps to store temporary data in storage. Use the following command to see configured swap file
[source]
sudo swapon -s
[/source]
To create a 256 Megabyte file. You can do this by specifying a block size of 1024 byte and a count of 256:
[source]
sudo dd if=/dev/zero of=/swapfile bs=1024 count=256k
[/source]
To activate the swap file
[source]
sudo mkswap /swapfile
[/source]
Add the swap permanently by adding it to the fstab file
[source]
sudo nano /etc/fstab
[/source]
Open and Paste in the following line
[source]
/swapfile none swap sw 0 0
[/source]
and then set the permissions
[source]
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
[/source]
Option 3 Increase the swap file size
if you already have swapfile in root directory and just try to increase your swap file space.
use the following commands to create 4GB swap file.
[source]
sudo swapoff -a
sudo dd if=/dev/zero of=/swapfile bs=1G count=4
sudo mkswap /swapfile
sudo swapon /swapfile
[/source]
That’s it.
Thanks
happy coding 🙂

Related posts:

2 Replies to “Fix for Nodejs – Cannot allocate memory and Process…”

Leave a Reply

Your email address will not be published. Required fields are marked *