-bash: fork: Cannot allocate memory

I also faced this issue with my Ubuntu 14.04 desktop.

 free -m

Even these basic command showed Can't allocate memory error. On investigating, found that system is using all the memory for Caching and is not freeing up memory. This is called Cache Ballooning and solved this by clearing the cache.


In my case, OS was running out of PID instead of memory, the error message was the same though.

The default value of max PID number is 32768, to view the value, run

cat  /proc/sys/kernel/pid_max

To change the max pid number, run

echo 100000 > /proc/sys/kernel/pid_max

In my scenario, the root cause was that one java prcocess spawned 18k+ threads( in linux kernel, thread is essentially a process), to find out the thread count of each process, run

ps -eo nlwp,pid,args --sort nlwp