Ubuntu Intermittent Freezing Fixed with Swappiness
Having run Ubuntu on my Dell XPS 14z for years I have been increasingly plagued by an intermittent freezing problem which causes the UI to freeze (mouse still movable) for anything from 5 to 30 seconds. I’m not sure when this started (maybe Ubuntu 19.04 time) but it has gotten worse with each subsequent Ubuntu release to the point that Ubuntu 20.04 was almost unusable which prompted me to search harder for a solution. The solution I found resolved the problem completely - Swappiness.
Mine is capable but aging hardware but it appears that the OS is Swapping memory to disk too agressively for my system which is what is causing the temporary freezing to occur.
The Linux Swappiness setting is an itensity setting on how aggressive the OS should swap memory to disk. Here is the offical definition:
“This control is used to define how aggressive (sic) the kernel will swap memory pages. Higher values will increase aggressiveness, lower values decrease the amount of swap. A value of 0 instructs the kernel not to initiate swap until the amount of free and file-backed pages is less than the high water mark in a zone. The default value is 60.”
For a great explanation checkout this great HowToGeek article.
Whilst the default is 60 there are differing opinions on what to set it to for certain types of hardware and OS usage but for me I went with a value of “20” and its been great. No more freezing so far.
Find your systems current setting with this command:
cat /proc/sys/vm/swappiness
Then update it with this:
sudo sysctl vm.swappiness=20
No reboot required for testing, and when you have decided on the final value to use you need to persist it via updating the sysctl.conf file, e.g:
sudo nano /etc/sysctl.conf
and set the vm.swappiness=20 setting in the file.
This solution worked for me and Ubuntu 20.04 is now running really well.