0

I use an SSD swap just to avoid full memory related crashes. There are limited erasures that can occure on SSDs. The memory related crashes have stopped, but my system uses swap constantly and vmstat shows in almost any period of time si and so != 0. I have changed my /etc/sysctl.conf to try to reduce the use of swap and increase pressure to dump cache, but I still find swap is used when there are Gbs of memory available.

I have already made some changes to try to reduce swap use:

$ cat /proc/sys/vm/swappiness
1
$ cat /proc/sys/vm/vfs_cache_pressure
200

There is a temporary solution to force swap into the cache in this ask ubuntu question, but this is not useful when I am not actively monitoring memory.

OS: Pop 20.04 focal, Kernel: x86_64 Linux 5.11.0-7612-generic

2
  • I've been running swapless for over 15 years now. Yes, I've had more than enough RAM but even if you don't, you could use something like earlyoom to avoid stalls/freezes in case you're really low on RAM. – Artem S. Tashkinov Apr 26 at 12:52
  • I was running swapless for ages, but then I had a few memory based freeze ups so I had to revert :( Probably a more sensible fix is more RAM! Thanks for the earlyoom tip. – James Apr 26 at 13:18
1

I suggest you delve deeper in to the underlying issue, the problem you are really trying to resolve, and other solutions for that issue. Or at least, better understand how the OS utilizes swap space.

Many people assume, incorrectly, that SWAP space is only used when free RAM is needed. Generally speaking (I am not certain about the Linux kernel!), an OS with swap space will swap out blocks of RAM which it sees have not been recently accessed: Very few algorithms will check if the swap is needed because RAM is full, only if the RAM has not been recently accessed. Thus, if you leave the system idle for awhile, RAM which has not been accessed because the system has been idle and certain programs are not accessing parts of their memory space, the OS will still swap out parts of that RAM even when there is still plenty of free RAM.

This is illustrated by this question over on Ask Ubuntu, where the user experiences the OS swapping large parts of RAM to swap space while leaving their system idle for days--then experiences poor system performance for awhile after returning and using the system again and the OS is trying to restore used/accessed blocks of RAM which are once again being accessed regularly.

One approach, if you can use it, is to not use the SSD for swap space at all. In my desktop, I have an old 500GB non-SSD hard drive with a swap partition I use.

Another method would be a performance trade-off: Create a cron job for a script that runs every minute and checks RAM use. When RAM use is above 60%, it uses swapon (with appropriate parameters) to use a swap file in case it is needed. When RAM use drops back below 40%, it then uses swapoff to disable the use of the swap file and let the OS "drain" the swap back in to physical RAM. Of course, you will need a previously created and properly initialized swap file to enable/disable. (And make the thresholds different... If you set them both to the same value such as 50%, you will experience thrashing when the RAM use hovers between slightly above 50% and slightly below 50%. Fine tune the values to how you typically use the system.)

I am sure there are other methods and techniques used by others, as well. Likewise, as the use of SSD drives increases, the kernel algorithms will likely be modified to use SSD-based swap partitions less unless absolutely needed.

3
  • Thanks! The cron approach is interesting, but might encourage more erasures than leaving it be which is the real thing that degrades the SSD. I think HDD might be a good solution for now. The system is online for months at a time, hence swap and cache are much higher than usual. – James Apr 26 at 10:10
  • 1
    The cron approach won't trigger though unless RAM usage is above the threshold. At that point, yes, it may trigger a lot of writes... which it is already doing as-is. The intent is to make it not use a swap file or swap space at all when RAM usage is below the threshold: No swap == No disk writes -- "law of averages" type trade-off. There are other issues, however, so as long as you have a secondary disk to use for swap partitions/files, that is the solution you should use. Laptops and embedded devices are the main issue where a secondary drive is not an option. – C. M. Apr 26 at 12:19
  • (And to add, the swap file approach does not erase the swap file, so there is no "write to delete" it; It just tells the kernel to stop using it.) – C. M. Apr 26 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.