Linux Swap | Different Kinds and How to Use It

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's go over swap files swap files are something of a misunderstood thing of Linux some people think you need swap partitions some people say you need swap files some people say you don't even need swap and really the answer is it depends so in this video we're going to go over that we're gonna explain basically the differences between partition and swap file and how to enable both how to use both and then if you even need to use it or maybe not use it as much as you do because at the end we want to maximize memory usage and not swap usage so with all that said let's jump into this video okay so let's jump right in here this is just a standard Linux installation with a swamp actual swap file in here so let's take a look at just the specs we have I'm gonna run an H top you haven't used H top before every distribution out there has it whether it's r j-- debian you name it just to install h top and you can kind of see what's going on with your CPUs but the big thing we're looking at is memory usage and swap usage you'll notice memory usage I have about 16 gigs of memory and a 14 gig swap file now I'm gonna tell you right now honestly if a 14 gig swap file on this system is kind of pointless because one it's not enough to do a hibernation so if you have over 16 gigs or 16 gigs in the up the only real reason to use swap files in a residential space like this or maybe if you're doing like heavy virtual machine work where you're utilizing probably more than half of your memory and also doing something on top of your you know operating system that you're running the virtual machine in so if you're not big on virtual machines and you're basically just using your Linux box for just regular stuff whether it be games video editing whatever it might be you probably don't even need a swap file so in this instance a 14-gauge swap file is kind of because that's all I really use this box for is just testing like this so I would probably actually just disable it altogether but we can also kind of talk about when you need swap files now swap files by themselves you need for hibernation which I just mentioned or like system standby and those types of things you need to have a big enough swap to cover all of your memory and technically you probably should have a little more than what you have in your memory just to make sure now on top of that it depends on your swap files some people always say have enough swap to cover your memory for just hibernation however when you get to like eight gigs that's probably a good thing like if you're an eight gigs you probably want about a 10 gig swap and if you're at 4 gigs or 2 gigs of memory you don't have much I would recommend probably double or triple what you have if you're at two gigs I'd probably recommend a 6 gig swap file if your four gigs of memory I'd probably recommend around an 8 gig swap file so just keep that in mind as far as just how big your swap file should be so in this instance let's let's do some stuff with this this actual system so we're gonna go ahead and kill this and we're gonna turn off swap altogether so right now I'm gonna just go ahead and pull up gparted just so you can kind of see the partition structure so you see right now I'm using a better FS for my main route my boot is this is actually a UEFI or an EFI system so it requires this and I believe this is also GPT so that's just kind of give you an idea of the scheme here and then we have our swap partition now I can't touch or change any of this just yet because we're utilizing swap so we need to first turn off swap to do this I'm gonna go ahead and close out of this we just do swap off and we can just pull up the help and just kind of show you the options so what we're gonna do is swap off - a-and verbose so this should disable it however we didn't run it as sudo so we'll do sudo alright so now we've disabled swap and what say I wanted to reclaim that so will relaunch gparted here and you'll notice it is no longer locked I can actually delete this and say okay cool so I'm gonna just go ahead and leave this right here I wasn't actually able to grow this backwards to move all this I would need to unmount this partition boot into not this environment and move all of my stuff forward that's gonna be a little bit ugly for the 14 gigs I'm just gonna go ahead and leave this as is but we're not using swap anymore however the swap partition still exists because any time you boot this system it's gonna look for that partition so we need to make sure from the system level we take that out so let's just do that real fast we're gonna nano into our etc' fstab let's just come down here and we're gonna comment that out because we're gonna actually come back to this and change basically how this is done all right so from here now we can actually create like a swap file so let's say we wanted to remove our swab and just to kind of verify your swaps off you'll see now that swaps not actually going but let's say we wanted to just make I'm only gonna make like a two gigabyte swap file or maybe even like a one gig swap file so we'll do sudo eff allocate - l1g and we're gonna put it as swap file in our route with this file created now we need to actually write zeros to it so it's all blanked out there's no fragmentation going on so this will create that's one gig swap file alright that's created and then we want to make sure that only root has access to it because we don't want a program getting into our swap file and seeing other information that's in there so we want to make sure from only a system level so we'll change how that is we'll go sudo chmod 600 so now we've written it we've set our permissions now we also need to make it a swap file so we do encase swap swap file this sets up swap space for it and then from here we can actually engage our swap file so we'll go sudo swap on four slash swap file okay so we found some stuff out after doing some research of why this wasn't working and wasn't anything with my distribution it's actually better FS better FS actually does what's called copy-on-write and it does this a lot that's how you get like really quick snapshots and other really cool features with better FS but natively better FS does not support swap files and what you can do is put like a no cow or no copy-on-write attribute to the swap file and go about enabling it that way however if you have better FS I wouldn't recommend it a lot of people are like this is super experimental not really very many people use swap files with better so long story short don't use a swap file with better FS is my recommendation as nobody else on the Internet really contributes on this actual thought process many people say it can be done however I just wouldn't recommend it because of how better FS is designed so swap files and better FS don't do it is my recommendation if you need it obviously just use a partition and just do it that way so even though the beginning of video is like there's no difference between the two look at that actually practicing it and doing it we discovered something new that's not well documented on the internet so anyways I thought this was kind of an interesting deal and that's why I showed it but honestly most people aren't running better FS and these steps can be followed completely to the end and you'll be completely fine so just know that as far as using swap files and swap partitions they're basically the same now with all that said we're just gonna do one final check here look up and just do a cat of our fstab all this looks good I actually marked out the old swap so we're good there and we'll just exit out and then we'll just do an H top as well and you'll notice that the swap file is off now since this is 16 gigs of memory I'm gonna continue to leave the swap file off however let's say you have a swap partition or a swap file honestly performance wise it doesn't matter let's say it's utilizing too much of the swap file let's say you had 16 gigs but you're big on vm's and you really only want to use that swap file when you absolutely have to use it so you can do something called VM Dot swap enos and that is actually gonna be in nano it will actually go to et Cie or nano et Cie systemctl Kampf now this is blank because there's nothing here but a lot of systems when you have a swap file has what's called swap enos so we're gonna go and just type this out real fast just so you can see it swap enos and this is usually equals 10 now this is is decent and there's not I this would say only use soir penis on the occasion when you you have to but for the most part run everything from memory now on older systems or systems with more swap space than memory let's say you have a 2 gigabyte memory and 6 gigs of swap file obviously you're gonna be doing a lot of swapping so you'd probably want this at like a 60 that's on the super high end on a very low-end machine that's what that would look like now let's say on my inside machine some people are like holy crap you got tons of memory and tons of swap space what the heck and sometimes I'll run three or four virtual machines all within that same PC and my swap enos actually looks like that it looks like a three because it's really not using my swap enos unless it absolutely needs to it's only using my swap partition because it needs to and honestly in that machine I could use a swap file again that there's no difference between the two but I only want to use that swap file when absolutely necessary so I turn it all the way down to three most people would recommend not going below a ten however I absolutely hate using the swap file and it's only there at a necessity so that's why I use it as a three and really at a three the only purpose it serves is for hibernation or system standby and those type of features so on this outside machine I usually always turn it completely off so I don't really care and I won't use a swap file for it but if you do use hibernation or system standby and you only really want that feature and not really utilize your swap file I would crank it down to something like this and that way you can have your feature but also not be bogged down by using swap file because obviously you don't really need it with 16 gigs of memory and you're not heavy on resource usage or you're barely ever tapping into so that's a good explanation as far as when you want to use a swap when you don't and you can tinker around with this value and figure out what's right for you and your system so that was swap in Linux I hope you guys enjoyed this because honestly it took a little bit of research because I was trying to really figure out exactly when one would be better than the other and a lot of times I would find a lot of this good information I was like okay this will make the perfect video for today but let me know your thoughts down in the comment section below and a big shout out to my patrons without you I wouldn't be able to make videos like this one and I'll see you in the next one
Info
Channel: Chris Titus Tech
Views: 38,656
Rating: undefined out of 5
Keywords: chris titus tech, swap, linux, swap partition, swap space, linux tutorial, linux swap memory, swap linux, swapon, swap in linux, kali linux, linux mint, mkswap, linux swap, swap partition in linux, linux mint swap, linux swap file, linux swap space, linux swap server, linux swap file size, linux swap tutorial, linux swap partition, how to create swap partition in linux, linux swap management, linux swap partitions, redhat linux, debian linux, swap memory
Id: HKy-KEtCT9o
Channel Id: undefined
Length: 12min 27sec (747 seconds)
Published: Fri Aug 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.