A couple of times recently I've noticed my laptop running a bit slow and the disk light flickering a lot. It's an i7 with SSD and 12GB RAM, so I'm not really expecting much slowness when just browsing.
This last time, it bugged me, so I had a quick peek in System Monitor (KSysGuard) and it was showing my 2GB swap partition to be 100% utilised, but physical memory only at 4.8GB out of 11.6 (which is where the graph goes to). So I thought I'd try to empty the swap by turning it off & on again (swapoff -a
, swapon -a
) but swapoff failed due to insufficient RAM.
This confused me, as I should have about 7GB spare, according to KSysGuard, so I thought I'd see what top had to say. Top showed about 150MB free RAM and 2048/2048MB swap in use. No wonder it was running a bit slow!
Why would KSysGuard under-report memory usage so much and how can I make it top telling porkies?
From the comments...
Note: remember that unused RAM is used for file and disk buffers.
The real problem is that your 2G swap space is too small, and your disk is thrashing.
I would increase your swap to at least 6G, and modify your vm.swappiness from the default of 60, to 80.
To test various settings of vm.swappiness, use
sudo sysctl vm.swappiness=80
. vm.swappiness values greater than the default of 60 make more use of swap. Values less than 60 make more use of RAM.To make it permanent, edit
/etc/sysctl.conf
and addvm.swappiness=80
at the end, then reboot.Per the comments, I increased my swap file from 2GiB to 6GiB using the following:
Respectively, the above commands turn swapping off by moving pages back to physical memory, remove the swap file, create a new 6GiB file called swapfile, set the permissions to root only, configure the file for swap use and finally turn swapping back on with the new file.
I then added
vm.swappiness=70
to the end of the penultimate section in/etc/sysctl.conf
to make swapping to the page file slightly more aggressive (saving some physical memory).However, to actually answer the question of why KSysGuard seemed to be lying about the amount of RAM in use, it's because the default "Memory and Swap History" chart actually only shows data from the "Application Memory" sensor. To resolve this, I added a tab called Memory in KSysGuard and included 2 graphs - one for physical memory and one for swap. The physical memory graph has the following sensors configured: Used Memory, Free Memory, Buffer Memory, Cache Memory. Only Used and Cache are really required, but it's the Used metric which tells you how much memory has actually been used.