SSH and -bash: fork: Cannot allocate memory VPS Ubuntu
Asked Answered
A

2

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.
Aileen answered 18/11, 2014 at 1:47 Comment(0)
S
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.

Sanjuanitasank answered 18/11, 2014 at 2:14 Comment(2)
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?Aileen
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?Sanjuanitasank
K
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.

Knife answered 2/2, 2019 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.