rails assets pipeline "Cannot allocate memory - nodejs"
Asked Answered
F

6

32

we've just upgraded to Rails 3.2.5 from Rails 3.0.7 and using the assets-pipeline compilation on the fly for the staging server, but some times we face this exception !

Showing /var/rails/appname/app/views/common/_my_partial.html.haml where line # raised:

Cannot allocate memory - nodejs /tmp/execjs20120613-17090-thoc8f.js 2>&1

Extracted source (around line #):

Trace of template inclusion: app/views/layouts/application.html.haml

Although nothing fancy or huge memory allocations is done in the coffeescripts or in the images folder for example !

Thanks...

Fanjet answered 13/6, 2012 at 11:19 Comment(2)
I'm facing a similar problem, did you get to solve yours?? thanks!Distracted
@yorch: See my comment below. It fixed the problem in 30 seconds for me.Chrysalis
C
83

It's simple to spend the three minutes (maybe two if you type fast) to add a swap file to your server.

If you're running Ubuntu (not sure how well this works for other Linux flavors), just follow this tutorial from DigitalOcean:

https://www.digitalocean.com/community/articles/how-to-add-swap-on-ubuntu-12-04

Voila!

Chrysalis answered 7/8, 2013 at 16:54 Comment(3)
I faced a similar problem when deploying with AWS Elastic Beanstalk, found this issue using SSH to connect to the instance and check the production logs, afterwards followed the tutorial and was fixed. Thanks!Mabelmabelle
@MustafaELBanna Would you please mark this as the accepted answer? Many other people think it is.Chrysalis
@KyleCarlson can you put in a link to your blog post anyway?Consociate
W
13

Based on the tutorial link provided by Kyle Carlson


Check swap space

sudo swapon -s

An empty list will confirm that you have no swap files enabled:

Filename Type Size Used Priority

Create and Enable the Swap File (swapfile)

sudo dd if=/dev/zero of=/swapfile bs=1024 count=256k

Create a linux swap area:

sudo mkswap /swapfile

output:

Setting up swapspace version 1, size = 262140 KiB no label, UUID=103c4545-5fc5-47f3-a8b3-dfbdb64fd7eb

Activate the swapfile:

sudo swapon /swapfile

check if you can see the swap summary.

swapon -s

Filename                Type        Size    Used    Priority
/swapfile                               file        262140  0   -1

Done!


To make the swap file permenant

sudo nano /etc/fstab

Paste in the following line:

/swapfile none swap sw 0 0

Swappiness in the file should be set to 10. Skipping this step may cause both poor performance, whereas setting it to 10 will cause swap to act as an emergency buffer, preventing out-of-memory crashes.

echo 10 | sudo tee /proc/sys/vm/swappiness
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf

set up the correct permissions on the swap file to not readable by the public:

sudo chown root:root /swapfile 
sudo chmod 0600 /swapfile
Wind answered 3/8, 2015 at 13:25 Comment(0)
V
11

Based on @tohi's answer, I created a script which you can paste into a terminal.

# Turn it (off) on
# sudo swapoff -a
sudo swapon -s

# Create a swap file
# 512k --> Swapfile of 512 MB
sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k

# Use the swap file
sudo mkswap /swapfile
sudo swapon /swapfile

# make sure the swap is present after reboot:
sudo echo " /swapfile       none    swap    sw      0       0 " >> /etc/fstab

# Set the swappiness (performance - aware)
echo 10 | sudo tee /proc/sys/vm/swappiness
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf           

# Change the permission to non-world-readable
sudo chown root:root /swapfile 
sudo chmod 0600 /swapfile

Update: If you need to resize the /swapfile at a later point check out this answer: https://askubuntu.com/a/763717/508371

Vilify answered 19/1, 2016 at 19:7 Comment(0)
F
1

We contacted the provider's support, and they are responded in 2 messages like this:

  1. There are two things that can be causing this: Either you are indeed running out of memory constantly or your Webby doesn’t have swap configured.

We have a sysadmin checking it and we’ll respond to your ticket soon.

  1. Your swap was disable for some reason, and that is why you were having memory issues. I fixed the fstab entry, and enable the swap on the right partition. You should be fine now.

And until now this error does not show :) Hope it will keep not showing for the future too ...

Thanks, and best of LUCK ...

Fanjet answered 9/7, 2012 at 12:14 Comment(0)
T
0

Maybe it helps to:

RAILS_ENV=production rake assets:clean

Restart your webserver / e.g.

service apache2 restart

Then:

RAILS_ENV=production rake assets:precompile
Talyah answered 8/3, 2018 at 12:39 Comment(0)
P
0

It seems that popen is pretty expensive. I am not sure whether you have .coffee precompiled or not, but if you have .coffee in your application as a view then Ruby converts it to JS, obviously. Coffee -> JS compiler itself is written with JavaScript, so it uses JS-runtime which is Node in your case. Probably there is a memory leak or smth like that caused by one of the ruby gems, thus it probably is not related to node.js directly.

I'd advise to ensure that:

  • you have all assets pre-compiled for production
  • you do not have views which must be compiled to JS on-the-fly with external compiler

Couple more notes:

  • If you are using Docker as production environment then it'll take some tricks to enable swap here, it may be somewhere deep inside your infrastructure
  • I would not recommend using disk in high load applications as a workaround, because discs are slower then RAM, though a gap may be not really significant but still present
Psychokinesis answered 22/4 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.