Score:0

How can I prevent Ubuntu from swapping when there is sufficient physical memory?

us flag

I have an Ubuntu 20.04 desktop system with 16 GB of physical memory and 8 GB swap.

After some uptime and somewhat heavy usage, I noticed swap space was almost fully used while physical memory was just below 50%. Consistently with that, I noticed poor performance when returning to applications that had been in the background for longer, complete with disk chatter – so apparently the system was busy reading memory pages from swap.

I then tried to force everything back into physical memory by running swapoff -a; swapon -a (which briefly brought memory usage just above 90%). However, as soon as swapping was back on, I noticed the system immediately started moving pages back into swap until swap usage was at about 60%.

This looks kinda inefficient to me and I would prefer data to be kept in memory while stocks last, to avoid the overhead of reading pages from swap when I need them again.

How can I influence this behavior? I have seen the swappiness parameter being mentioned, but as I understand it, that just gives the system the order in which pages are to be evicted from physical memory, not how soon to start evicting pages.

in flag
[This answer](https://askubuntu.com/a/1317009/1222991) might answer some of your questions about swappiness
cn flag
and do read this: https://askubuntu.com/a/1379/15811 and the remark: "Just because swap is allocated, doesn't mean it's being 'used'. Whilst programs like system monitor and top will show some of your swap space being allocated that doesn't mean that the system is actively swapping in/out."
us flag
Since you have a lot of memory, you can use zram instead of swap. You won't anymore experience slowness.
Score:1
cn flag

That is Linux being economic with memory. You have high memory usage at some point. Thus, linux swaps memory to the disk. When later, you have less memory requirements, linux leaves the swap on disk, so the memory is available for disk cache and for anything else you are doing, or plan to be doing. Only when you need the memory residing in the swap space, will linux retrieve it from swap. Not earlier. Since you do not need it anyway.

Lowering swappiness effectively will reduce the tendency of the OS to swap pages to disk, and increase the tendency of the OS to cache disk files to memory.

that just gives the system the order in which pages are to be evicted from physical memory, not how soon to start evicting pages

Not quite correct, in my opinion. Memory management is complex in reality]. This is the definition from the Linux documentation source:

"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.

You can indeed lower swappiness: it will reduce the tendency of the system to swap out paged memory. In return, your hard drive will have more work physically writing files, because these will be cached less. What the net effect is on disk strain, depends on actual usage.

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.