Score:1

Linux using swap when RAM is available

au flag
s5s

I am experiencing an issue where linux seems to be using swap instead of RAM. To me it looks like I've triggered a bug because:

  1. The swap seems to be used for storing data that is regularly used, not hardly used.
  2. The entire PC seems very slow to respond to any command (e.g. opening the start menu, switching windows, starting a new terminal etc.)
  3. swap memory usage is 100% when I've got 28GB (twenty eight) of RAM left (4GB used)
  4. The swap is used for applications that I just launched (e.g. chrome, R-studio) because it goes up when I launch these apps and is freed when I close them.

Is there a way to force linux to stop using swap for new memory allocation? I've set the swappiness to 0.

EDIT: Before I asked the question, I changed swappiness via sudo sysctl vm.swappiness=0 and I've made the change permanent in /etc/sysctl.conf. This didn't make a difference. I haven't rebooted my PC because I've got jobs running but up time is just 3 days.

$ cat /proc/sys/vm/swappiness
0
$ uptime
09:08:54 up 3 days, 6 min,  1 user,  load average: 1.90, 2.23, 2.45
$ uname -a
5.4.0-72-generic #80~18.04.1-Ubuntu SMP Mon Apr 12 23:26:25 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
$ free -m
              total        used        free      shared  buff/cache   available
Mem:          32022        1891         229         610       29901       29069
Swap:          2047         988        1059

EDIT2: I am running a SQL script against postgresql DB. The script is very long but made up of just a number of \copy(select ...) statements which dump data to csvs. I would imagine it's not psql that is hammering the swap but the kernel which decides which memory to use?

Someone avatar
my flag
Have you changed swappiness permanently? Note: it resets every time you reboot.
in flag
Could you [edit] your question to include the Terminal output of `cat /proc/sys/vm/swappiness`?
au flag
s5s
@matigo Added the contents.
in flag
`4GB used` are you running 64bit and/or HWE kernel?
au flag
s5s
@PvdL 64 bit standard linux kernel - see question updated.
FedKad avatar
cn flag
Can you add `free -mt` output to your question?
au flag
s5s
@FedKad Added `free -m` but this is after I executed `sudo swapoff -a && sudo swapon -a `. The swap used before this was 1.8GB (out of 2G available).
Raffa avatar
jp flag
Related: https://askubuntu.com/q/1347219/968501
Score:1
cn flag

After you set swappiness to zero, you can try:

sudo swapoff -a && sudo swapon -a 

to transfer the swap space to memory without rebooting the computer.

au flag
s5s
I think this seems to have done something that helps. Will update in a bit.
au flag
s5s
`sudo swapoff -a` did it - made swap unavailable so whatever was using it stopped using it.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.