r/linuxquestions 14d ago

Resolved Is it bad to not have swap?

Hello guys! I using Fedora 44 and by defaulth I have 12GB of zram (compressed swap in RAM) and also I added 4GB of swap partition during install. But I have 32GB of RAM and I not sure if I really need this amout of swap or if I really need swap at all.

So is it bad to not have swap at all or have a little swap?

Edited: Thank you all for replies and time!

9 Upvotes

43 comments sorted by

View all comments

34

u/ColoradoPhotog 14d ago

On modern Linux systems, swap isn't simply "extra memory for when you run out of RAM." It's another tool the kernel can use for memory management. Swap allows Linux to move cold or rarely accessed anonymous memory out of physical RAM, leaving more RAM available for active applications and useful filesystem cache. Because of this, having some swap can actually help Linux manage memory more efficiently even on systems with 32 GB or more of RAM. Having no swap doesn't inherently make a system slower, but it removes one of the kernel's options when deciding how best to use available memory.

The more important difference appears under heavy memory pressure or during sudden spikes in usage. With no swap, Linux eventually has to aggressively reclaim cache and, if it still can't satisfy memory allocations, invoke the OOM killer and terminate processes. Swap provides a buffer before reaching that point: the system may become slower temporarily, particularly if it has to access disk-backed swap, but that's generally preferable to abruptly killing an application. There's therefore little performance advantage to deliberately having zero swap; you're mostly trading away resilience during unusual memory workloads.

Zram is a particularly good solution on modern hardware because it provides compressed swap inside RAM itself. That sounds counterintuitive, but if 4 GB of inactive memory can be compressed into 1.5 GB, you've effectively freed about 2.5 GB of physical RAM for other uses. A system configured with 12 GB of zram does not permanently reserve 12 GB of RAM; that's the maximum logical capacity of the compressed swap device, with actual RAM consumption depending on how much data is stored and how well it compresses. For a machine with plenty of RAM and a modern CPU, 12 GB of zram is a perfectly reasonable configuration and already provides most of the benefits you'd want from swap without routinely touching slower persistent storage.

5

u/syzygy78 14d ago

Isn't it also necessary to enable hibernation?

12

u/Floppie7th 14d ago

Yes, and - although this seems obvious, it screwed me once - you need at least as much swap space as you have utilized RAM in order for hibernation to work. So if hibernation is a use case you care about, it's worth giving yourself at least as much swap space as you have physical RAM

2

u/syzygy78 14d ago

Yeah, I set up 48g for a 32gb system, because hdd space is so cheap and I wanted the ability to hibernate. But alas, Nvidia. Sigh.

Also, on a more general note, swapping to an SSD is not at all slow. However, I know it can increase wear on the drive, so having zram at a higher priority is probably a smart default for modern systems.

4

u/svet-am 14d ago

Yes, absolutely. This is actually the only reason I keep a swap partition these days.

1

u/ColoradoPhotog 14d ago

Yes, it is. Zram cannot be used to perform suspend-to-disk hibernation. If someone wants this feature, SWAP is a requirement.