Thoughts on swap file vs swap partition use

a few months ago when i did a fresh install to bodhi linux 5.0 (based on ubuntu 18.04), i noticed that it was setup with a swap file rather than using the swap partition that i already had installed and that the previous version (based on 16.04) had used. it didn’t take a whole lot of search and effort to get it set back to using the partition again, but i also read in that process that ubuntu itself had changed to using a swap file over partition in their setup.

i feel like the partition makes more sense for me because i have three distros installed, but was wondering what others here did with that option :slight_smile:

1 Like

I never encountered an OS install that didn’t automatically recognize and use an already existing swap partition. Well, it’s Ubuntu(-based), though, so who knows what they are up to, again. I personally prefer a partition rather than a file. I found some info regarding this here.

1 Like

when i installed debian on my third partition, it recognized the swap partition right away. could definitely just be a ubuntu(-based) quirk like you said :slight_smile: interesting points in that link. resizing the swap partition (trying to get bodhi and ubuntu mate to hibernate. debian does so without a hitch) did require changing fstab to get it to recognize the new one. took me a few tries to figure out blkid and uuid and get that all sorted correctly.

I actually 100% approve of them moving from a swap partition to a swapfile… step in the right direction…

1 Like

It’s still not Linux-like to force something upon the user. If I already use partition, I expect it not to be silently replaced.

do you mind my asking what you feel the advantage(s?) of the file over the partition is? truly asking to further my own knowledge. i’ve only been using linux for about 18 months and my first install just added the partition so that was what i have gotten used to. i understood from @Akito’s link that one of the possible advantages of the file is that it is easier to change the size which makes sense.

He also mentioned, that the hard drive header doesn’t need to move around that far when the swap file is physically close to the normal files on the file system. Though, to me this is a little random. I feel like having a partition gives you more control. Resizing it isn’t really an issue from my perspective, because if I make a swap partition it is always bigger than I expect to need. And even if you need more swap temporarily only, as far as I know there’s a way to use a swap file + partition simultaneously.

to be fair, that could have totally happened due to user error/lack of knowledge. the other day i was reading an article abhishek linked to on itsfoss.com where he went through ubuntu install steps. he mentioned needing to signify in that process that the swap partition would be used by the new install. i cannot say whether or not i did so. this was my first time upgrading via fresh install and i absolutely could have missed an important step :slight_smile:

Well, as I said, usually the installation detects and uses it automatically. I use only Debian-based systems - currently running Debian 10 on my computer - and the installer of this OS always automatically searches and uses the swap partition. No questions asked, no silent replacements.

one of the interesting parts of getting my system switched back over to the partition was that i had also read in the interim that it should be larger than total ram (i have 8 gb. some guides suggest 11 for hibernation) so i did go ahead and increase it. i was unaware that doing so would change the uuid so that left me with 0 swap briefly until i realized what had happened. in that same process i also saw that having both file and partition listed in fstab gave me access to both like you mentioned so i definitely see where adding some through that method temporarily would be easy enough.

i was curious about debian (interesting experience so far) after a year plus using ubuntu and ubuntu-based systems so i installed that to my third partition. i have tried vm’s but prefer the full install experience. as i stated before, debian set up the partition without any input from me that i remember.

If youre unexperienced with GNU/Linux, then Ubuntu based systems like Mint are a good starting point. After a while though, once you want to do advanced stuff, I tried so many different systems and finally settled on vanilla Debian, because it proved itself of being by faaaaaaaaar the best general purpose distribution out there, in my opinion. Right now, the only reason I wouldn’t use a Debian-based distribution for something I want to do, is if there is a very specialized and extremely well made distribution already out there that is not Debian based in the corresponding scenario. Examples would be opnSense, Porteus Kiosk or something that isn’t even based on Linux, like Haiku OS and React OS.

I’m a newbie and after reading this thread, it got me to thinking that if I put the swap partition on a second HDD, it would speed up the seek/write IOs as it can read from one drive while writing to the other drive…I had a second HDD and put a swap partition on each HDD which defeated the purpose of having the swap on a different HDD. What do you guys/gals think?

2 Likes

Depends whether you’re likely to be using swap at all… most of my Linux devices have 12 or 16 GB of RAM - and I almost seem to never even touch swap… I often end up turning it off anyway … hence why I prefer to have a swap file over a partition (instead of wasting a partition)… e.g. on the desktop I’m using right now (Laptop running Bionic) - I’ve disabled swap completely…

If your machine is 4 GB or less - by all means have a swap partition or swapfile… 8 GB? Maybe? Start off with a 2 or 4 GB swapfile - if you find you’re never using it - remove/disable it. 16 GB? Probably no need…

If it was a server running an Oracle database? I’d probably have a great chunk of disk (or series of chunks) allocated to swap device…

3 Likes

i am also fairly new to this area, but your idea seems to make sense to me. like raid 0 where distributing data is done to increase write speed. of course that would also depend on what @daniel.m.tripp pointed out: how often do you really use swap and is there any actual speed gain. fun thought/experiment to run just to see though :slight_smile:

2 Likes

super valid point. i realized when i read this that i was surprised the other day when i looked and saw i had used a whole 250 kb of my swap. not even sure what i was doing at the time that it kicked in. it is rare that i use even a quarter of my 8 gb of ram. i’m just not an intensive user. i like a minimal de and tend to close any windows i haven’t used in the past hour or so.

2 Likes

I keep on average about 20+ (sometimes more) tabs open in Google Chrome (e.g. two Outlook OWA tabs, couple of service management system pages, work’s sharepoint, this forum, my gmail, couple youtube tabs) - and was barely “touching the sides” of my swapfile - so I disabled it and deleted it…

KiB Mem : 16312356 total,  3983196 free,  5030724 used,  7298436 buff/cache
KiB Swap:        0 total,        0 free,        0 used. 10290252 avail Mem

that’s with 16 chrome tabs, two RDP sessions (both remina and xfreerdp) to Windows server jumpboxes, about 4 or 5 terminals (with tmux), Synergy KVM and Sayonara playing music, dropbox and Resilio Sync running : on Ubuntu 18.04.1 with a fairly “vanilla” gnome DE, two external DP 24" monitors at 1080p…

2 Likes

I am lucky enough to have 16GB of Ram and if I remember right I made the swap partition 16GB as well. I would like to see the usage of RAM vs Swap. What command and syntax will show me that data? Thanks for helping a newbie like me.

2 Likes

I mostly just use a random sample of the output from “top”…

There’s also “free” (i.e. the command “free”)

3 Likes

Thanks! I tried it and it worked.

1 Like