Ubuntu – Incredibly reluctant swapping


A weird problem has appeared on my laptop running Ubuntu 16.04. I have 8 GiB RAM and 10 GiB swap area. It worked fine, programs were swapping fast from the SSD, the swapped memory were mostly memory leaks and forgotten mmap anyway… but then something broke it. I have no idea what, I have not noticed it immediately. But now, it swaps incredibly rarely.

The swap space is almost unoccupied, I can have 95% RAM usage and swap space is only 5% full. If RAM fills up, it rarely swaps anything out, it freezes instead, sometimes it recovers after killing a program, but way too often, I have to do a hard reset.

I have checked the swappiness property, but it is set to 60 and setting it to 100 changes nothing. I upgraded Ubuntu to version 17.04, but it didn't help.

If I run a process that takes a lot of memory and uses it actively, it appears to swap out the memory of this process (that just allocated it!), making the process freeze again soon to get back the memory shortly aferwards, in a never ending loop. Almost none of the swapped out memory remains swapped out for long, although I am pretty sure there is enough leaked memory and unused programs to swap.

Any ideas what might be wrong?

Best Answer

Having checked sysctl -a | grep swappiness to ensure that swappiness is set as desired, and swapon to ensure that the swap partition is mounted, there remains a possibility that the swap partition has been improperly formatted for some reason.

This can be corrected by

sudo swapoff -a
sudo /sbin/mkswap /dev/hda8
sudo swapon -a

where /dev/hda8 should be replaced with the partition or swapfile that is suspected of being bad.

The sources for this answer include https://help.ubuntu.com/community/SwapFaq in the section "Why is my swap not being used?", and Swap not being used

Related Question