I would like to understand why is Ubuntu swapping while the memory is underutilized. I have plenty of RAM available, so I would not expect any swapping at this point.
-
At some point you had enough memory usage to drive things into swap. And once things get shunted off to swap, they stay there until something actively uses it. So whatever's in swap right now hasn't been used by any running process for a while. – muru Mar 26 '24 at 04:54
-
1If you're not happy with the Linux kernel's default swappiness behavior, change it. https://help.ubuntu.com/community/SwapFaq – guiverc Mar 26 '24 at 06:35
-
2Does this answer your question? How do I configure swappiness? – guiverc Mar 26 '24 at 06:35
2 Answers
You have more than enough RAM, and will not need swap. In principle, you could decrease swappiness so that the system will use it less, but practically, having another two gigabites of swap is really not that helpful.
Just remove the swap. First, turn it off with sudo swapoff -a
.
Open /etc/fstab
, and comment out the line beginning with /swapfile
.
E.g., convert the line
/swapfile none swap sw 0 0
to
#/swapfile none swap sw 0 0
Then, reboot.

- 28,338
- 18
- 105
- 212
According to this document: https://www.kernel.org/doc/gorman/html/understand/understand014.html
swap has two main purposes. one of them is:
The casual reader may think that with a sufficient amount of memory, swap is unnecessary but this brings us to the second reason. A significant number of the pages referenced by a process early in its life may only be used for initialisation and then never used again. It is better to swap out those pages and create more disk buffers than leave them resident and unused.
that might answer your question. why is it not on the RAM but on the disk? system decided it would be better. that might be the main reason of your RAM being underutilized at the beginning, smart swapping.

- 101