1

I am a bit confused because everyone says to reduce the swappiness to about 10; but if I have only 4GB RAM than how much swappiness would be the best? and also If I had 8 and 16GB ram?

and also I am not talking about swap partition/file size I am talking about swappiness.

[Edit]: I don't want to know that what swappiness does, instead I want how much I need. According to a suggestion below I am listing some common tasks and apps I use. I am a programmer so apps like: VScode, Pycharm are neccesary for me and also some heavy tasks like virtualization (most of the times of android or windows) are in my usual work. And I also do video editing and graphics design, but, for those I use light programs (I think they are because they feels so fast) like: Shotcut, Inkscape.
So, I think this info would help in answering the question

Will appreciate an answer!:))

Unnat
  • 155
  • 3
    This is impossible to answer. Depending on actual application and usage patterns, the best value would be different. Just test a few settings yourself and see which works best. If you don't notice a big difference, it probably just doesn't matter as much. – Sunday Feb 20 '21 at 11:21
  • 1
    Swappiness is there for emergency. Therefore avoid using swap if you can. Swap is 1000x slower than RAM – Bruce Malaudzi Feb 20 '21 at 11:32
  • @jsbillings but how much?? – Unnat Feb 20 '21 at 14:03
  • Do you intend to ever suspend to disk? – jsbillings Feb 20 '21 at 14:06
  • @jsbillings Nope – Unnat Feb 20 '21 at 14:10
  • I think 4-6G of swap is fine for 8G of RAM, and less if you have less RAM. Suspend to disk relies on having enough swap to write all memory to, so that’s when you’d want parity – jsbillings Feb 20 '21 at 14:11
  • 1
    @jsbillings Ok let me clear, As I said i am not talking about swap size, but, about the swappiness .... – Unnat Feb 20 '21 at 14:13
  • 1
    Just leave it at the default. I doubt you will notice. – jsbillings Feb 20 '21 at 14:14
  • 3
    "I don't want to know that what swappiness does, instead I want how much I need.". It's a ratio/score not a quantity. If you want to change it the least you could do is know what it does first. – A.B Feb 20 '21 at 16:57
  • Information: Wikipedia Swappiness may be worth reading. Also https://help.ubuntu.com/community/SwapFaq (section 8). This value is essentially a trade-off between using RAM for processes or for disc cache, so depends very much on your workload characteristics. It is anyway a very blunt instrument that only comes into play when RAM is close to full. – Paul_Pedant Feb 20 '21 at 17:46
  • @jsbillings: https://www.howtogeek.com/449691/what-is-swapiness-on-linux-and-how-to-change-it/ – X.LINK Feb 22 '21 at 01:07
  • Check out this answer for more details: https://unix.stackexchange.com/questions/88693/why-is-swappiness-set-to-60-by-default – jsbillings Feb 22 '21 at 02:23
  • Example 1 and 2 are not what a normal desktop user will face. Example 3 is what I said about SSD wear. – X.LINK Feb 22 '21 at 02:37

2 Answers2

-1

Set it to 10 you probably won't even notice a difference during normal use cases maybe when your compiling but you can always use sysctl vm.swappiness=X and try out a setting for a little bit while your working most people are not going to notice a major difference in performance. If you concerned about SSD life check out one of the many posts and articles about it basically it "may" have been an issue at one point in time but that time has come and gone when your dealing with even a marginally good drive. The predicted life for a 128 GB MLC SSD of medium quality (3000 write cycles before failure) and average of 10GB write per day is around 35000 days so we are dealing with absurd times here. Don't over think the issue.

Jason C.

Just to bring it up in case your interested in this stuff look into trim for SSD's. Can start a whole new interesting discussion.

  • The thing is, that almost no consumer-grade SSDs are MLCs since five years now because they're mostly all 10x nm TLCs with worse lifespan, and now we have QLCs already starting to replace TLCs which are even worse. Theoritical cells lifespan is a thing, but reality is another one since write-based manufacturer warranties is far shorter than the "year-based" ones, some OEM rates them as only worth of 100TB writes. Writing 250GB of data a week is very easy with web browsing nowadays (e.g Youtube and JS infested webpages that are the norm helps a lot), even for a granny. – X.LINK Feb 22 '21 at 02:46
  • Also, most field-tested articles we have are only about "3D" 40nm TLC drives (roughtly 2/3 lifespan of planar MLC 20nm) which also none of them are produced since around four years ago (the lower the nm, the less it will lasts). Those "field-tests" were also done with empty drives, which is far from reality. We have no field-tested data about the lifespan of "3D" 20 nm TLC cells, nor the 1xnm ones or even the QLC ones. That, and no field-tested data retention for any SSD celles type while the theoretical ones are pretty low. Anyway, swapping on SSD is a bad idea. – X.LINK Feb 22 '21 at 02:49
-2

You're starting things with a wrong assumption: Swappiness don't change with how much RAM you have, but with what software you'll use.

If you do virtual machine, you don't want a high swappiness. But if you do graphics design or coding, swappiness may be high.

But as a rule of thumbs: Set your swappiness to 0, so Linux will swap as late as it can. Doing this will keep your system responsive, and prolong your SDD lifespan if you have one (that's why you mostly saw answers to set swappiness to 10 and nothing else).

As others say, swap will only be an emergency "RAM" for most people.

Also, considering that you only have 4 GBs of RAM, you'll pretty quickly reach that limit in no time just with web browsing alone, swap won't even help here. Even 8GBs of RAM won't be enough for what you intend to do.

Then, depending on what your CPU is, you may still have a hard time even with more RAM. To make it short: there is a relation between your CPU and how much RAM you should have, but also what you intend to do (which you told us already).


Some tips about swap:

  • Swap partitions are useless for most people, do use swap files instead.
  • Swap should be at the very least the physical size of your RAM (not how much the OS detects them) PLUS the amount your dedicated GPU(s) physically has, if you have one. That's even truer if you do make use of hibernation.

Also: Just because a program looks "fast", doesn't mean it's lightweight since caching is a thing that is overly used since less than a decade. In short: You'll trade less CPU usage for far more RAM usage.

X.LINK
  • 1,314
  • 1
    Swappiness of zero is just a waste of perfectly good RAM. – jsbillings Feb 22 '21 at 01:10
  • There's no reason to swap until it is need, which means when there's not enough RAM. Preemtively swapping means making the system less responsive while nothing else fills the RAM, while sometimes thrashing the I/O and reduced SSD lifespan for nothing. – X.LINK Feb 22 '21 at 01:25
  • 1
    Pay closer attention. I asked if they intend to suspend to disk, OP said No. Also, I think you might be confused by what swap is used for. It’s not just “emergency memory”. A normal OS uses swap for memory pages that haven’t been touched in a while, and lets the kernel use unused RAM for buffers and cache. With no swap, or swappiness=0, those pages are essentially wasted on storing objects that no one cares about. – jsbillings Feb 22 '21 at 01:28
  • Also, last I checked, Video RAM wasn’t suspended to disk. Anything important in vram needs to be put into RAM by the driver enforce hibernation or regenerated on restore. – jsbillings Feb 22 '21 at 01:31
  • Also, since you do bring up hibernation, swap files are a major pain to beginner users (adding kernel parameters?), and can break restore completely if you use btrfs. – jsbillings Feb 22 '21 at 01:54
  • Still hasn't answered the question first, OP asked for swappiness, not how much swap he needed (which it why I put the amount question in the "tips" section).

    Yes, VRAM does get stored to disk (page 36): https://download.lenovo.com/pccbbs/mobiles_pdf/tp_p53_hmm_en.pdf#pdflink-5ab0927d

    It's all up to the implementation, so the worst case will apply since some OEMs do sometimes (way too often in fact) have their own set of modified GPU drivers or even non-standard way of doing things ingrained in the BIOSes, which means yes so no risks are taken.

    – X.LINK Feb 22 '21 at 02:17
  • Also, no kernel parameters needed: https://askubuntu.com/a/892410 . Swap partitions are even a bigger pain to beginners: extending the swap partition should they need it while they sometimes have to shrink other partitions (e.g. /, with potential data losses) and must sometimes be done with a liveCD, swap partition using too much space for nothing, etc. Most beginners won't use BTRFS, but you can then create a swap file on any other existing partition while not having the same usual swap partition have. – X.LINK Feb 22 '21 at 02:29
  • Then, swapping to allow Linux to buffer and cache is only needed when RAM is getting short, not before that to prevent I/O thrashing and disk wear for nothing. – X.LINK Feb 22 '21 at 02:29
  • I said use the default swappiness, it’ll be fine. Btrfs is the default filesystem to two major distro, OpenSuSe and Fedora. Again, you are confused about how the Linux kernel uses RAM, and how Linux performs suspend-to-disk in general. I don’t want to be overly harsh, but what you are saying doesn’t track with how the Linux kernel works. Quoting a Lenovo manual (for Windows!) isn’t exactly a good source. – jsbillings Feb 22 '21 at 02:42
  • I'm not confused about how Linux kernel uses RAM (sometimes badly, e.g. OOM bug, etc) and how it performs suspend to disk. The discussion was about swap partitions or swap file, and also why swapping (most often far too early) to allow it to cache/buffer/prefetch is not a good idea for normal uses. So nah, everything is fine. Concerning the Lenovo manual, it's still better than a "we don't know, it depends on the driver", better assume the worst case and take it as a yes with that. Again, OEMs do weird things at bios (which Linux drivers may have to cope with), and at driver level. – X.LINK Feb 22 '21 at 03:05
  • Fedora is not really a beginner distro, same goes for OpenSuse that is easier on that part but people will mostly land on Ubuntu. – X.LINK Feb 22 '21 at 03:06
  • Linux doesn’t handle low memory conditions well, I’ll agree. But that isn’t what we are talking about. If you want a (dated) reference to how hardware handles suspend, there’s docs in the kernel: https://www.kernel.org/doc/html/latest/power/video.html . Linux suspend only writes out a copy of what is in RAM, that is my only point for suspend. As for swap, you are saying the same incorrect thing about how it is only used in extremely low memory. – jsbillings Feb 22 '21 at 13:11
  • 1
    @jsbillings your first comment here seems to assume that swappiness 0 prevents the system swapping "opportunistically". it doesn't. If you meant something different I'd be interested to know how you think it wastes ram? – Philip Couling Feb 26 '21 at 11:19