Running any command returns "Cannot allocate memory" on Ubuntu Server

Solution

As it says in the error messages, your machine has run out of memory. This can be for a number of reasons, but basically, something is eating up all of your memory and not leaving any left for even basic command usage.

I would suggest that you reboot your droplet (just go to your client control panel and select "Reboot"), ssh in and then run top or htop. Keep an eye on the memory usage and see what process is using up all the memory. From there, try either

  1. Killing/Removing the faulty program/process

    WARNING: PLEASE do your research on if the process is an essential system process, first! If a system process is causing memory issues, don't just kill it, do research on it and for specific ways to deal with it.
  2. Changing configuration for that program/process so that it doesn't eat up all of your memory.

Suggestions for preventing the issue from happening again

  • Something good to do is to add swap memory, as it allocates more memory if you're running out.
  • Whenever you install programs, make sure you configure them correctly so that they don't perform in unintended ways (like eating up memory)
  • After each time you add a package or basically anything new is configured, check with htop or top to see how much memory you're using up with the current programs. If you notice that you're using almost all of it, try and clear some out by going through and removing unnecessary programs/processes.
  • If there is anything that's being auto-started (besides system processes, of course!) that you don't recognize or want to be auto-started, remove it! But always do your research on what a process is before killing/deleting it, as it could be essential for bootup procedures or system functions, etc.

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

Reference

  • How does the OOM killer decide which process to kill first?