35
How much swap? (lemmy.ca)
submitted 1 year ago by sam@lemmy.ca to c/selfhosted@lemmy.world

I've heard a lot of people say your swap should be 2x RAM.. but do I really need 32GB of swap?

top 44 comments
sorted by: hot top controversial new old
[-] wgs@lemmy.sdf.org 22 points 1 year ago

Don't even bother with a SWAP partition. Create an empty file on your / partition so you can grow/shrink it as needed.

did if=/dev/zero of=/SWAP bs=1024m count=4
mkswap /SWAP
swapon /SWAP
[-] float@feddit.de 5 points 1 year ago* (last edited 1 year ago)

This is the way! There's a catch with swap files on encrypted disks and hibernation but that's quite a special case. Edit: forgot to mention zswap, the compressed version of swap.

[-] mygreatlimbo@lemmy.world 5 points 1 year ago

Not a golden rule. For example btrfs has limited support for swapfiles.

[-] wgs@lemmy.sdf.org 1 points 1 year ago

Of course there's no golden rule. But I've found this one to be pretty useful ! Didn't know about btrfs limitations though, thanks for the hint!

[-] eh@nerdbin.social 8 points 1 year ago* (last edited 1 year ago)

On a desktop or especially laptop case, it should be equal to (or larger than) your RAM if you use hibernation (as RAM gets copied to swap during hibernation)

On my server, I set it up to be 2GBs, mostly arbitrarily. Right now it's at 500MB, but my main memory is also only 600-800MB full out of the total 4GBs available, so I'm not running out of RAM anytime soon.

Swap behavior seems to have changed a while ago, so consider reading https://chrisdown.name/2018/01/02/in-defence-of-swap.html on how it works right now. Hell, even that might be outdated nowadays. Up to date info on how swap works really seems hard to come by.

That was very informative, thanks.

[-] Corngood@lemmy.ml 1 points 1 year ago

Also make sure to count GPU memory, as that will have to be written to swap on hibernate.

[-] leopardboy@netmonkey.tech 7 points 1 year ago

It’s something that Linux users have been saying for 20 years and it’s outdated. It makes sense when maybe your computer came with less than a GB of RAM, but these days I usually configure a server with a small amount of swap (like a couple of GB), and I set swappiness to something very low like 5.

[-] frustbox@lemmy.world 7 points 1 year ago

The 2× recommendation is indeed way old, it stems from a time when computers had 1 or 2 GB of RAM or even less. Nowadays, if you have 16GB of RAM I'd say you're not going to need it (in most use cases).

Chances are your RAM will rarely get so full that your system will need to swap to disk, it's probably going to clear buffer/cached data first. This is data kept in RAM that's not actively used by the system but might be useful soon.

If your RAM does run full you either have some very specific application that demands it (then you probably already know the importance and hopefully wouldn't ask internet randos) or you have a memory leak - that's a problem and I don't believe swapping helps in this case. It's way too slow for that.

If you run VMs and reserve RAM for each those considerations might change.

Personally I don't think swap partitions are particularly useful any more, certainly not 2× your RAM. If you ever want to suspend your system, then it needs to store all your RAM content to disk and it will use your swap for it, so 1× your RAM would be required. But with modern systems and SSDs booting only takes seconds, so I don't think suspend to disk has much utility.

For me swap files are a good compromise. But if a system with 16GB starts swapping, something is not going well.

[-] vegetaaaaaaa@lemmy.world 6 points 1 year ago

Even if you don't run into RAM exhaustion, it is always good to have some swap space, as it can be used to move less used pages to swap, and frees up RAM for more filesystem cache.

I always keep a 2GB swap partition.

[-] eager_eagle@lemmy.world 2 points 1 year ago

right, swap usage is controlled by the swappiness value. With the default value of 60 out of 100 and almost never filling up my 32GB RAM, my swap of 8GB is always used at least a little bit, and depending on the uptime it eventually gets "full".

[-] eager_eagle@lemmy.world 4 points 1 year ago* (last edited 1 year ago)

Personally I don’t think swap partitions are particularly useful any more

This is just wrong. Having swap increases the performance of your applications as it frees up RAM by swapping cold pages out of memory and use that space for caching, which increases hit rates. And this performance increase is both for low and high used memory rates.

[-] SheeEttin@lemmy.world 2 points 1 year ago

So once it gets pushed out of RAM, you read it from disk instead of from disk? I'm not sure that that's a benefit. If you're using SSDs, they are fast and don't have seek times, so I don't think that having it in swap is any faster than just from disk. Maybe a little overhead for opening the file.

Assuming that you aren't truly exhausting your RAM, anyway, such that programs start crashing due to being unable to allocate. Or that the data on disk is compressed, for example.

[-] eager_eagle@lemmy.world 4 points 1 year ago* (last edited 1 year ago)

the swap space works as another level of memory hierarchy, the kernel swap pages out before all RAM is used in order to make room for pages that are more likely to be requested. It is very much a benefit, especially when using SSDs.

so I don’t think that having it in swap is any faster than just from disk.

That is still is faster, because even though they have the same physical access times, the data stored in swap space is already mapped to memory pages. By not using swap you are just more likely to face lower hit rates for low memory usage and thrashing for high memory usage. The slowness of the disk won't affect your overall performance as you'll get higher cache hit rates.

Right now for example, roughly half of my RAM is being used for caching and all of my swap is used. If I didn't have swap, most of that space used for caching would instead be used for those cold memory pages.

[-] frustbox@lemmy.world 1 points 1 year ago* (last edited 1 year ago)

I have a swap partition in my current system (64GB), I've had one on my previous system (which was 11 years old, with 16 GB) - I have seen my system¹ swap at most 5 times, and every time it was software misbehaving so badly that it made the system unusable.

I know this is anecdotal evidence - but my experience is that it's getting less and less relevant for most desktop systems.

Edit: ¹my old system, the new one has not swapped at all in the past year.

[-] cardes@social.pi.vaduzz.de 2 points 1 year ago

@frustbox @sam I mostly agree, you should always aim to only use ram and not swap with your system. That beeing said you can have swap usage in any kind of system and even if you have terabytes of ram there is still a valid point for having some swap because that's still better than a kernel panic. Also in desktop computers you can get 16-32GB RAm full while gaming and multitasking some stuff along.

[-] warmaster@lemmy.world 1 points 1 year ago

I have 16gb, I play Overwatch 2 through Bottles (Wine) and my ram usage & swap partition are almost always full when playing.

[-] __lb__@feddit.nl 6 points 1 year ago

I believe the swap partition is also used when hibernating. So when you use hibernate you need at least equal swap space as memory size.

[-] ARNiM@lemmy.world 6 points 1 year ago

As most have pointed, the “always 2x” rule doesn’t have that much of relevance in 2023 as most computers now has more than 4GB of RAM. I would only use that much of a swap when using a low hardware.

For desktop, I would never go swapless, though. In the event of memory pressure, swap would still help in that situation so that OOM Killer do not kick off and unintentionally kill my working process. Plus it helps that Linux can move the least used data to the swap and use the RAM for filesystem cache.

So my rule of thumb, for desktop: If RAM < 8GB: Swap == 2x RAM If RAM => 8GB: Swap == 1x RAM

For servers, I think it depends on the workload. I keep a small amount, like probably 50% of RAM or less. But for stuff like Redis, it doesn’t make sense to have swap. You want to ensure that everything is in the memory.

[-] massive_bereavement@kbin.social 1 points 1 year ago

This was pretty much on point.

I would add not using swap for hypervisors and virtual servers as that can cause noisy neighbor issues and tend to increase iowait pressure.

[-] InEnduringGrowStrong@lemm.ee 1 points 1 year ago

Yea my server is running 128GB of RAM, no way I'm making 256GB of swap.

[-] fireduck@lem.trashbrain.org 1 points 1 year ago

Yeah, on servers low swap and an alert on available ram so I know if things are getting snurgly.

[-] SJ_Zero@lemmy.fbxl.net 5 points 1 year ago

There's people who make dumb arguments that go "but what if you run out of ram?" And then someone else says "I have enough ram" and then someone else goes "but what if you run out of ram?"

I have a small amount of swap, a few gigs, and enough memory for the application. Moreover I also have my swappiness set to 0 because I don't want stuff swapped out of memory. If I need more memory I need more memory.

[-] takeda@kbin.social 6 points 1 year ago* (last edited 1 year ago)

I'm one of those people.

I will leave you this: https://chrisdown.name/2018/01/02/in-defence-of-swap.html

This is a nice read from a kernel developer responsible for memory management.

[-] dpflug@hachyderm.io 1 points 1 year ago

@takeda
Thanks for sharing. Always good to learn more.
@sam @SJ_Zero @selfhosted

Hi there! Looks like you linked to a Lemmy community using an URL instead of its name, which doesn't work well for people on different instances. Try fixing it like this: !selfhosted@lemmy.world

[-] eh@nerdbin.social 4 points 1 year ago

Moreover I also have my swappiness set to 0 because I don’t want stuff swapped out of memory. If I need more memory I need more memory.

I don't think swappiness has worked that way for a while now.

https://chrisdown.name/2018/01/02/in-defence-of-swap.html

[-] toikpi@feddit.uk 5 points 1 year ago

I suggest that you read "In defence of swap" that various people have linked to. It includes information about swap size.

Here are swap size recommendations for from Red Hat and Canonical. You may not run oof these distributions but the information will probably still apply.

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/storage_administration_guide/ch-swapspace

https://help.ubuntu.com/community/SwapFaq

Assuming you have 16GB of RAM 32GB is the maximum swap size you need if want to use hibernation. You can you less if don't plan to use hibernation.

[-] fiasco@possumpat.io 5 points 1 year ago* (last edited 1 year ago)

I think it's better to think about what swap is, and the right answer might well be zero. If you try to allocate memory and there isn't any available, then existing stuff in memory is transferred to the swap file/partition. This is incredibly slow. If there isn't enough memory or swap available, then at least one process (one hopes the one that made the unfulfillable request for memory) is killed.

If you ever do start swapping memory to disk, your computer will grind to a halt.

Maybe someone will disagree with me, and if someone does I'm curious why, but unless you're in some sort of very high memory utilization situation, processes being killed is probably easier to deal with than the huge delays caused by swapping.

Edit: Didn't notice what community this was. Since it's a webserver, the answer requires some understanding of utilization. You might want to look into swap files rather than swap partitions, since I'm pretty sure they're easier to resize as conditions change.

[-] cmeerw@programming.dev 5 points 1 year ago

Unused (or very rarely used) memory can also be swapped out to make more memory available for the disk cache (in the hope that having more disk cache available will save more disk accesses than the (hopefully one-time) swap-out operation).

[-] Secret300@lemmy.world 5 points 1 year ago

I usually just have a swap of like 4 or 8 gigs on disk and use ZRAM as well. I set the priority so it uses ZRAM first and if that fills up then it'll use the swap partition. I don't think it has ever used the swap partition tho

[-] axzxc1236@lemmy.world 4 points 1 year ago

I setup Swapspace in case I ever need to compile something very big, but usually my system doesn't use swap.

[-] alvaro@social.graves.cl 4 points 1 year ago

@sam@lemmy.ca Honestly it depends a lot on your use case. I have 1x RAM swap and usually only half is used

[-] snailtrail@lemmy.world 3 points 1 year ago

If it's a laptop and you want to be able to hibernate, swap must be large enough to hold system memory, plus a little extra just in case. Other than that, everything depends on the workload. Generally, no. Maybe a few gb in most cases.

[-] manitcor@lemmy.intai.tech 3 points 1 year ago

depends on what your apps are doing, a lot of the servers ive installed i seem to need to force to use more memory instead of some internal disk cache it already has. its adjustable and youll know when you need it.

[-] misaloun@reddthat.com 1 points 1 year ago

I use gentoo, so my swap is pretty large because it often compiles from source. I have it at 16 GB for my raspberry pi and 32 GB for my pc. I think I'll be bumping up the raspberry pi to 32

[-] ThorrJo@lemmy.sdf.org 1 points 1 year ago* (last edited 1 year ago)

I'm currently running 16GB zramswap on my 32GB server, and 24GB on Optane / 3D XPoint on my 64GB server 😈

[-] takeda@kbin.social 1 points 1 year ago* (last edited 1 year ago)

I think this might answer your question: https://chrisdown.name/2018/01/02/in-defence-of-swap.html (especially the tunning section)

load more comments
view more: next ›
this post was submitted on 07 Jul 2023
35 points (97.3% liked)

Selfhosted

40394 readers
348 users here now

A place to share alternatives to popular online services that can be self-hosted without giving up privacy or locking you into a service you don't control.

Rules:

  1. Be civil: we're here to support and learn from one another. Insults won't be tolerated. Flame wars are frowned upon.

  2. No spam posting.

  3. Posts have to be centered around self-hosting. There are other communities for discussing hardware or home computing. If it's not obvious why your post topic revolves around selfhosting, please include details to make it clear.

  4. Don't duplicate the full text of your blog or github here. Just post the link for folks to click.

  5. Submission headline should match the article title (don’t cherry-pick information from the title to fit your agenda).

  6. No trolling.

Resources:

Any issues on the community? Report it using the report flag.

Questions? DM the mods!

founded 2 years ago
MODERATORS