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
- 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. - 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
ortop
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?
In completion to the accepted answer there is one additional thing to consider: Your system may run out of file handles or even socket buffers and still have lots of memory whilst giving the same error. This is especially true if the shared hosting imposes limits of such nature. On OpenVZ systems, watch the contents of
# cat /proc/user_beancounters
This will give you in the rightmost column the first overruns. If this is true, either move to a larger hosting package or hunt down the most likely culprit: the mysql or mariadb database which may, in presence of a defective PHP app, leak file handles to the hundreds per second.
This may also happen if your webserver has ssh open to the internet and accepts username / password logins: even if you have fail2ban running, you may have attracted a distributed dictionary break in attempt which also consumes a lot of resources.