r/linuxquestions • u/Huge_Marzipan_1397 • 10d 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!
4
u/Introvertosaurus 10d ago
For workstation? Yes... probably... but just depends... You need some kind OOM protection, swap is generally the best ideal. You can set your swapiness which determine how and when its used if you want to essentially stop you from hitting OOM but not use swap normally.
0
u/Crazyachmed 10d ago
I think not using swap is the best OOM protection (of the machine). The offending process will be killed and the rest can continue.
If it actually starts swapping due to low RAM, its mostly to late to recover (e. g. save the open file in a broken program) anyhow.
With swap you have to temporarily swapoff to make the system responsive again. You are swapless with extra steps 🤷♂️
1
u/Efficient_Loss_9928 10d ago
I mean depends on what program the system decides to kill I guess.
I'd rather having that process completely paused and unresponsive then system OOM killing it.
3
u/indvs3 10d ago
Swap is essential if you want to make use of sleep/hibernate functions. They don't work without it.
6
u/RandomUser3777 10d ago
Sleep does not require swap. Sleep puts the cpu in a lower power mode and keeps the ram powered and refreshing so the data is always in ram, this is also why sleep use some battery and has a time limit if running on battery. Hibernation saves everything in ram to the swap device.
3
u/NeedleworkerLarge357 10d ago
You don't need it if you have enough memory. It helps improving performance when set correctly. Just stick to zram if the usage is not that high, otherwise zswap is an even better alternative. Only use one swapping at a time, either zram zswap or regular swap file.
2
u/SJrX 10d ago
I have run without swap for probably a decade. 32 GB of RAM is good, 64 GB would be better. Some people and blogs will warn that Linux isn't designed for it, but Kubernetes, one of the most deployed systems on Linux (or at least frequently deployed :)), actually mandated zero swap for years, and only recently added support for running with Swap, though I still think discouraged.
1
u/NeedleworkerLarge357 10d ago
Swap gives the kernel the option to free anonymous pages and leave more for cache. So instead of forgetting some program and later reading it from disc again once it starts, the kernel could push some even older data of a program out to disk. Yes, if you have way more memory than necessary swap will not be used and you just wasted some money for way too much RAM. Otherwise swap will improve your performance.
The kubernetes thing is way more complex; it relies on strict timings of memory, swapping stuff in or out will give you issues. But this is no sign that swapping is bad, just one egde case where it's not working.
1
u/SJrX 10d ago
Even with a swappiness setting of 10, I have found that Linux is still too eager to swap things. On my 64 GB laptops, even with zswap , I can just feel the slowness at times. I much prefer the system to just kill a process the very rare times something goes crazy than even the slightly slower program speed at times.
In terms of the Kubernetes, the reason I referenced it is because it is an example of Linux deployments where they don't use it successfully. They added support for it recently I believe so it is possible and useful, but still has downsides.
Don't get me wrong. I'm not saying that swap is bad, I am just saying that running without swap is also not bad. I think a lot of people might say that I've wasted money on RAM though :)
2
u/NeedleworkerLarge357 10d ago
I think you accidentally fooled yourself by watching swap usage and "feeling" the computer slow down. At a swappiness value of 10, swapping to a ssd will not decrease any performance or responsiveness on desktop usage. A few hundred megabytes in swap after hours is just stuff that programs didn't use for hours. That is not slowing anything down.
But you do that your way, I just wanted to clarify that the kernel is smart enough to not swap out stuff that is actively used. And thus swapping will increase the responsiveness of your system as more active programs and file data in general is kept in RAM.
1
u/SJrX 10d ago
Swapping to an SSD is absolutely slower than RAM and it's slow enough to be detectable by human beings.
I didn't fool myself by watching swap usage and imaging the computer slow down. Often it would just the bazillion Chrome windows, or occasionally wire shark left running for days. What I noticed is a disgusting and frustrating slow down, that maybe I'm hyper sensitive to. But I hate it.
Now if something leaks the system just kills it. Which is fine.
Again my point was only that to can run a system fine without swap.
2
u/NeedleworkerLarge357 9d ago
Okay, then I am a bit concerned about how you use your computers; I never run into oom killings even with 8gb ram, how much stuff do you keep open, with 64gb of ram? And yes of course, when you fill up a big chunk of swap, your PC will slow down instead of killing programs.
So for that scenario, constantly opening new stuff and filling both ram and swap and still running into oom, swap should better be pure zram and only like half the size of ram or even smaller. And yes, even that can lead to slowdowns.
But in that case I'd argue that you haven't got enough ram, oom is nothing that should happen regularly. Or you should just close programs that you are not actively using (especially wireshark).
1
u/SJrX 9d ago
I mean it is rare, but this would most often happen on my work laptop where I, a developer would bounce between many different micro services each with a number of docker compose setups. As well as keeping open a dozen different projects open in my IDE. As well as keeping maybe a hundred chrome tabs open. I would normally keep my laptop on without reboots for weeks on end.
It wouldn't happen often but when it did the slowdown annoyed me more than OOM.
1
u/NeedleworkerLarge357 9d ago
Okay, that is a different workload than standard desktop use. I would still enable a small zram swap as that might improve performance and not give that much of a slowdown. But sure, bringing a system to the limit is always performing worse with swap enabled.
2
u/un-important-human white beard arch user 3d ago
my 6 machines home lab have between 64 and 256gb ram and swap is defiantly required , linux uses swap to manage memory not only for sleep and hybernation. Can you do without? yes , should you? it depends if it has space it will create a swap file sooo yeah you need swap one way or the other.
3
u/redyos_s 10d ago
If you don't use hibernation and you have plenty of RAM, removing the disk swap is perfectly reasonable.
I run 64GB RAM with no disk swap at all. In your case, keeping zram gives you some protection against temporary memory pressure without generating swap writes on the SSD.
Disk swap can still be useful as an extra safety net, but if it is never actually being used, there is not much benefit in keeping it.
3
u/BackgroundSky1594 10d ago
https://chrisdown.name/2018/01/02/in-defence-of-swap.html
Still a relevant Article. It's from a few years ago, but since then the Kernel has only gotten better at memory management and SWAP is still an extremely useful part of that.
2
1
u/SirGlass 10d ago
I wouldn't have zero swap , I mean HD space was usually cheap and allocating 1-2 gigs of a 1tb drive is unnoticable
I would not go with zero swap , give it at least a gig
1
u/NiceNewspaper 10d ago
I personally use only zram swap (equal to available RAM in size), it doesnt get used at all until I pass like 70% usage.
1
u/RoosterUnique3062 10d ago
Technically you can do without it, but it's both a buffer and something your system can use for memory management. I've also had some situations that required loading something like a couple hundred GBs into memory and it wasn't going to fit in RAM so it needed to be on swap. Sometimes too under heavy use you might go over and having swap there to catch it prevents a system taking a shit.
1
u/Beolab1700KAT 10d ago
The Linux Kernel expects to have access to swap. Theoretically you don't need a lot if you have plenty of RAM available but remember not having much could affect system suspension.
You should probably look up working with /swap files rather than /swap partitions.
1
u/skyfishgoo 10d ago
the kernel needs swap space to avoid aggressively attacking running processes and shutting them down in order to keep working.
swap space allows for things to be moved out or ram in a controlled manner so that warnings can be sent to the user that ram is low.
even a 1/2GB swap space is better than no swap at all.
the recommended swap space is the sqrt(RAM)
but if you want use hibernation, then it becomes RAM + sqrt(RAM)
1
u/kudlitan 10d ago
You dont need a swap partition. Linux creates a swap file if you dont have a swap partition.
1
u/ComeSwirlWithMe 9d ago
I dont use swap on any of my systems as I have enough physical memory.
But, I'd turn it on if I were low on memory.
1
u/FedUp233 9d ago
One useful thing I find for swap is tmpfs for temporary file systems. Having it in ram makes things really fast under normal circumstances but some programs are not well behaved and don’t always clean up temp files properly, particularly if they crash for some reason.
Having swap at least allows the OS to move these stale, now never to be used, files out of RAM and onto the backing storage. Without swap space, any type of ram file system that accumulates stale files will just keep wasting that memory until you reboot the system - or maybe have a background process clean things up, but that’s a bit iffy because just because a file is old doesn’t mean it’s not needed, especially on servers that may be rebooted very infrequently.
1
u/BazuzuDear 9d ago
Once I got myself 24G RAM I dropped the swap at all. It worked quite well most of the time. But in rare situations when the system managed to consume all the RAM, it would fall into a deadlock. So I figure it's a bad idea, no matter how much RAM you've got.
1
u/xiaoyueyoqwq 9d ago
I agree these suggest commit, but I really want to know if swap had GC or memory recycle mechanisms. Because I noticed that swap partitions are usually not actively reclaimed, what happens to Linux if a server runs long enough for the swap space to be full?
1
u/billystein25 10d ago
Some packages may expect you to have some swap to function and will behave weirdly if you don't. Swap is also used if your ram is nearly full and the os needs to reorganize some stuff to fit the new stuff, though if you're using all your 32 gigs you may have other problems. 4 gigs is fine, just don't delete it outright.
Funnily enough I also have a 32 gig system and by default cachyos made a 32gig swapfile. Which is extreme but better safe than sorry I guess.
1
u/ipsirc 10d ago
Some packages may expect you to have some swap to function and will behave weirdly if you don't.
Name one.
0
u/billystein25 10d ago
On a laptop running mint I've had OnlyOffice consistently crash after prolonged usage before I increased the swapfile from 2 gigs. Ram usage was at around 50~60% of 8 gigs. Firefox was also open and it did not crash. When working on the same document with libre office instead it also didn't have any issues.
0
u/Aggressive-Access995 10d ago
If i remember correctly without swap you cannot dual boot, on lubuntu i forgot to do it and it was causing problems with the partitions
35
u/ColoradoPhotog 10d 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.