7

I am hosting my Rails app on Ubuntu 12.04 VPS, Nginx + Unicorn, after deployment everything is fine, but few hours later, when I ssh to the VPS I get this message

-bash: fork: Cannot allocate memory

-bash: wait_for: No record of process 4201 

-bash: wait_for: No record of process 4201 

If I run any command, it would just return

-bash: fork: Cannot allocate memory.
Nakilon
  • 34,866
  • 14
  • 107
  • 142
user1883793
  • 4,011
  • 11
  • 36
  • 65

2 Answers2

12

Seems you have run out of memory. Many VPS servers are setup with no swap, so when you run out of memory, it will kill things off in a seemingly random manner.

The easiest way to fix it is to get more memory provisioned to your VPS, likely costing more money. The next best way (other than running less stuff and memory optimizing everything running) would be to add a swap partition or swap file.

For a 1GB swap file (as root):

dd if=/dev/zero of=/swapfile bs=1M count=1024
mkswap /swapfile
swapon  /swapfile

Be sure to add it to /etc/fstab too as:

/swapfile none swap defaults 0 0

That will make it come back after reboot.

Beirdo
  • 414
  • 8
  • 15
  • Thanks for that, I thought about that too, but I got 1G Ram, and a fresh Rails app deployed with no traffic, how could it used up its memory? – user1883793 Nov 18 '14 at 02:40
  • My suspicion would be something in the rails app is keeping data unnecessarily in memory when it's done with it and maybe the garbage collection can't reap it? – Beirdo Nov 18 '14 at 02:43
0

To get out of this condition without rebooting, you can trigger the OOM killer manually as follows:

echo 1 > /proc/sys/kernel/sysrq
echo f > /proc/sysrq-trigger
echo 0 > /proc/sys/kernel/sysrq

Having done so you can inspect dmesg to find the process responsible for taking all your memory.

Dark
  • 227
  • 3
  • 9