Ubuntu – How to prevent the system from crashing or becoming unusable due to high RAM usage

17.10lubunturam

Sometimes when I use Lubuntu 17.10 – fully updated, my RAM usage hits 100% and the system becomes unusable, if I'm "lucky" I can move my mouse about 1 pixel per 10 seconds… This makes it practically impossible to do anything even shutdown an applications. I am then forced to use the power button on my laptop.

I'm using a macbook pro retina late 2013, with almost fresh Lubuntu 17.10 install.

So far it has happened while I have spyder3 open and I've loaded too big files. It also happened when I clicked this link in firefox (WARNING: might crash) http://stats.oecd.org/restsdmx/sdmx.ashx/GetData/SNA_TABLE1. I even closed the tab before hitting 100% usage, but it just continued up to 100% and crashed. I only noticed because I closed the tab and saw RAM usage in spyder3 (I was not running any python3 scripts).

Is there a way to prevent this? Perhaps make sure a specific amount of RAM can only be used by the OS?

Best Answer

Probably your problem is caused by the system "thrashing" – moving many pages of memory to and from swap space at once, and not leaving time for real processes to execute.

If you want processes that use too much memory to be killed rather than making your whole system run slowly, you can disable swap. Running sudo swapoff -a will achieve this until a reboot; to disable swap permanently, you need to edit /etc/fstab to remove the swapfile/swap partition, by removing or commenting out the line with swap in the third column. For example, my /etc/fstab looks a bit like this:

# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/mapper/ubuntu--vg-root /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
UUID=fdedeca9-dafe-1a05-0866-7502fda1a7ea /boot           ext2    defaults        0       2
/dev/mapper/ubuntu--vg-swap_1 none            swap    sw              0       0

To disable swap, I would comment out the last line (by putting a hash # at the beginning of the line), then reboot. Make sure you don't modify any of the rest of the file, otherwise you might make your system unable to boot.

Caveat: If you disable swap and don't have enough physical memory for basic system services, the OOM killer may decide to kill one of those and lead the system to crash or become otherwise unusable (which the question says you want to avoid).

Related Question