Is Swap partition necessary at present?
I have these partitions for my Linux distro: boot, root, swap
Swap partition is 2 GB while my RAM is 16 GB. I have a widget showing info in realtime about CPUs, RAM, Swap on my desk.
Recently I was playing Path of Exile and I saw my Swap partition was being used at 100% while RAM was nearly 4 GB. My question was: Should RAM not be used mainly while Swap is used just in case only? Something similar happens when using an Android emulators (Genymotion). Do I need any further configuration for my Swap? what if I don't have a SWAP partition?
UPDATE: I use SSD of 1 TB
Edited 16 Sep 2022. We only hold this version.
About this post
- Posted
- Archived
- Edited
- Score
- 35 · 91% up
- Kind
- Text
- ID
- xfruda
46 comments
This is the best article on the topic of swap that I know:
https://chrisdown.name/2018/01/02/in-defence-of-swap.html
Everyone needs to know about this page. Otherwise you will think that swap space is just emergency/extra memory (spoiler, it isn't).
Best arguing for swap. Indeed.
You need a swap space but it can be a file, im using zram, it's good and it's safer since it's inside my encrypted drive
Just to add on, swap file might be a better choice over a partition especially if you might need to resize down the road
Can do that also on partition, even if it's tricky. At least, tricky if you do not use LVM
zram is entirely in memory. It's definitely not inside your encrypted drive.
In any case zswap is strictly superior most of the time and is even enabled by default now.
Just FYI to anyone, there are some particularities to swapfiles and btrfs (or probably any post-ext4-gen fs). It used to not be supported but that's not the case since kernel-5.0 but there are still some caveats.
https://btrfs.readthedocs.io/en/latest/Swapfile.html
I have 32 GB RAM and I still keep a swap partition, even though it is rarely used. I have seen it used, though, when compiling large programs (such as chromium). I have no idea how gaming might use it.
If you have plenty of drive space, why worry about it?
I've wondered the opposite. I also have 32GB of ram and I saw it wasn't paging at all really so I just disabled swap altogether. I haven't had any negative effects from this.
disk space is not really a concern, but I always wondered if swap usage just caused a lot of wear on SSDs.
Of course It does. On home or office PCs is negligible, but on servers or workstation contributes more to its wear. Also, a swap partition will always use the same drive blocks with limited rotation. A swapfile on a large partition is preferred, due to a more room to rotate used and free blocks.
Well, there's no wear and tear when it isn't used. And if it's used only rarely, it's not much wear and tear.
You can reduce your system’s swappines so it prefers ram over swap https://wiki.archlinux.org/title/Swap#Swappiness
Swappiness doesn't affect the evaluation of memory pressure, but changes linux's affinity for anonymous or file backed pages on reclaim.
Read the wiki page carefully and you'll see it's talking about the usage of swap space. It is admittedly not worded all that well, though.
I haven't set a swap in years and have never had a problem.
That's not to say that a day won't come when having a swap would save me an unscheduled boot right at that very moment I don't want one, but given my experience thinking about swap is an unnecessary step.
There is no doubt an excellent argument why everyone must set a swap and that the failure to set a swap divides good people from slobs but - no. I don't care.
I'm with ya. I haven't used a swap *anything* in about six years on my main rig. (The laptop, with far less ram, is a different story. heh.)
At the end of the day, it is the user's decision. For good or ill.
That's a bit hyperbolic don't you think?
But I mean yeah, it won't cause problems in the sense it won't break your system, just like disabling one CPU core won't break your system, but still, why would you?
How is what like disabling a CPU core? And how is it similar?
Having some kind of swap is always good, mostly if using software eating a lot of RAM. Some say, when the drive is an SSD, it's better not using a swap partition in favor of a swap file or zram.
All my machines are running zram, it's working well.
Interesting about ZRAM, if I decide to use zram, what happens to my swap partition? what settings need to be made? a guide for dummies please
It's quite easy. First of all remove (or comment out) your swap partition entry from
/etc/fstabto disable it from next reboot.(optional) disable swap for your current session with
swapoff /dev/<yourswappartition>where <yourswappartition> is your current active swap, you can find it withcat /proc/swapsFor zram I chose the easy way: installing
zramdscript from AUR which will take care of everything. Just use your favourite AUR helper (like yay or paru) to install it, eg.paru -S zramd.After install make sure zram will be enabled at every boot and start it with
sudo systemctl enable --now zramdReboot to verify all went well and it started automatically. If the output of
swapon --showshows something like/dev/zram0under NAME column it should be all good, with the same command you will see also the zram size and priority.By default it dedicates to zram up to 8GB of ram. With a maximum allowed size equal to your physical memory size if lower than 8GB. Don't worry, it's ok to use all ram: it just compresses ALL memory pages, taking way less physical memory space. If your CPU isn't from ages ago it won't impact at all on overall performance. Anyway, you can change default values editing
/etc/default/zramdfile then reloading the service withsudo systemctl restart zramdFurther info on dev GitHub page: https://github.com/maximumadmin/zramd
Do you need a partition? No, a swapfile is also a valid option.
Do you need some form of swap space in general? Almost certainly, yes.
How did you get PoE to use so little ram? I typically see it go WAY higher lol
I just installed it by using Steam with Proton Experimental.
Don't you need a swap partition (or file) for hibernation?
Yes. If you want to use hibernate you need swap space.
Use zram. it's better.
Sounds like adding more swap space might be a good idea. You don't even need another swap partition, Just create a swap file in whatever directory you want and add it to the end of your fstab.
Necessary? No. Useful? Yes.
Even on high-end machines, it's useful to have swap. It's more about anonymous vs file pages management, rather than using it as "emergency memory", but i don't remember much at this point.
You also need it to hibernate the PC, iirc.
Looking into switching to a swap file.
You might not need a swap partition, but you want to set up a swap file
My take is that Swap is not necessary anymore (when talking about PC with common usage) but for what it takes (namely a few GB files or partition) I don't really see why you wouldn't do it.
Deleted before we saved it.
yes
Swap is used to keep the most frequently accessed files and program data in RAM, as I understand it. Generally on an HDD you want to avoid swapping as the disk is very, very slow at giving the data back when needed. On an SSD it's a lot more practical at extending your RAM if you don't have enough.
You also need a swap partition to hibernate.
You're being down voted because that's not what swap is for. Swap is a way of increasing memory available to processes so that if you run out of RAM you don't have to completely purge it. You're right that this is very slow and to be avoided.
If that's not what swap is for, why does the swappiness tunable exist? When swappiness is higher Linux prioritizes keeping the page cache (which holds files) in RAM. When it's lower it prioritizes discarding the page cache to maintain more program data in RAM.
Swap is definitely not needed on modern hardware running typical home/office/gaming type applications. Modern systems do a much better job at memory management, and have a lot more memory to work with, so having free ram and unused swap is not really a big deal.
Yes, and a big part of that memory management is to move unused memory pages out of memory, so that more memory space is available for caching things (and thus improve overall system performance).
And the space where those inactive pages go is… the swap space.