Linux Crash Course - Understanding Memory and Swap Usage

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello again everyone and welcome back to my linux essentials series in this series one video at a time i'll teach you foundational concepts around linux the things that i think that every linux administrator should know as part of their job or even your hobby in this video in particular we're going to take a look at memory and swap usage now normally i go through one concept at a time in each video but it's really hard to talk about memory and swap separately since they're so interrelated so in this video i'm actually going to cover both more specifically we need to understand how to tell how much memory we have available on our server so that way we understand if memory is filling up and we need to take action or if everything's okay and also what about swap sometimes it's highly debated when it comes to whether or not you need swap but i'm going to throw that into this conversation as well and it's going to be a great video but before i get into that i want to take a moment to mention the sponsor for today's video lenode the node is a linux-focused cloud server provider and using their platform you could actually set up your very own cloud linux server in minutes you could use the node's platform to set up your own blog perhaps a next cloud server a minecraft server but if you're not feeling particularly creative today then you can actually use their platform to spin up a linux instance that you can use while going through the tutorials in this series in fact they have all kinds of distributions available so if it's covered on learn linux tv then you could probably find it on lenode and best of all cloud servers on linux platform are very inexpensive you can get started with a linux instance for just five dollars a month but it's even more affordable because if you use the url that's on the screen right now then you'll be able to set up your brand new and you could fit quite lenov few linux servers within that credit the credit is actually good for 60 days and i highly recommend you check out lynnote's platform because they are awesome so definitely check out lynette's platform i highly recommend it and i really appreciate their continued sponsorship of learnlinux tv so without any further hesitation let's go ahead and dive in [Music] so right here on my studio pc i have an ssh session open to four different servers i actually just had this window open when i recorded a different tutorial but since i left h-top running i might as well start there h-top is an application that i covered in a previous video so check that out if you're interested in h-top and how it works but you could probably glean what it does just by looking at it i mean it's a resource monitoring utility it's showing me how busy my cpus are you can see that i have two of those it gives me the average of the cpu usage i've added that meter myself that's not normally there there's more information in the h-top video anyway more relevant to our topic today is the memory usage and judging by these numbers right here you could tell that i have over one gigabyte of memory free so if i didn't know any better i would say that this server is probably fine memory is good you could use h-top video over we're all set let's move on well actually one of the issues here is that h-top is not always going to be on your server most distributions actually make it available for you to install however typically it's not installed by default in some distros such as centos for example they don't even offer you h top in the standard repository so you're not always going to have this available so if we take this out of the equation then how do we actually tell how much memory is actually free without having to install any third-party utility well that brings us to the first command in today's video the free command the free command tells you how much memory is free so when you execute it you'll have all the information you could ever want to know about how much memory your server or workstation is actually using now i don't know about you though these numbers right here aren't the easiest to understand one thing that i do want to show you real quick is that those numbers are actually a bit easier to understand if we execute free dash m well the numbers are certainly smaller but what free dash m allows us to do is show the memory usage in terms of megabytes and that's actually a lot easier for me to read but now that we know to use the free command to find out how much memory is free which value is the one that we should pay attention to should we pay attention to the free field the available field maybe we need to look at the used field and do some calculation based on that how exactly are we supposed to digest this information right here i mean if we look at the free field here we have roughly 189 megabytes free so if this is actually the number that we need to pay attention to well things don't really look all that good do they but thankfully it's this number right here that we want to pay attention to this number tells us how much memory is available so we have 1040 megabytes available to us right now so wow i'm relieved because i was thinking right here that we had 189 megabytes free but since we actually have over a thousand free well i think i'm pretty comfortable but anyway the first takeaway of today's video is that under the available field that tells us how much memory is actually free and that's the number within the output here that we should pay the most attention to and actually you could probably argue that i could stop the video right here and send you on your way use the free command and pay attention to the available field and that's all you need to know well actually there's more to the story than just that but when it comes to memory management on a linux system that's quite the rabbit hole there's a lot of information there a lot of detail now while you could dive right into that rabbit hole and understand in greater detail all the ins and outs of memory management in linux i don't necessarily recommend that unless you really do want to dive in deep and if that's what you want to do then well i encourage you to do exactly that but what i'm going to do in this video is give you some more basics to help you understand i'm not going to go too deep into that rabbit hole myself but there are other values on that screen and we should actually understand what they mean so let's go ahead and see a basic definition of what the other values mean in the output of the free command okay so in this section i'm going to assume that you want more information when it comes to what all of these fields mean right here and again i'm not going to go too deep into this rabbit hole i'll give you the basics and that should be good enough and then if you want to explore in more detail how memory is managed in linux you could do that but if nothing else let's see if we could get a better understanding of what these individual values mean now first of all and i probably should have mentioned this earlier right here we have the total amount of memory that's installed on the server so we have about four gigs of ram it's always going to be a little less than what it actually is i'm not going to get into why that's the case it's not going to be that much different so if you see a value of 39.25 right here you have four gigs of ram again there's reasons why it's showing a lower amount than what's actually installed but we're not going to get into that in this particular video now this number right here that one's probably self-explanatory is telling us how much memory is being used and well about 2.6 gigabytes or so is being used on the server at this time but let's go ahead and tackle this confusion right here i mean we have 189 megabytes free but then we have at least as far as this output is concerned 1040 megabytes available but what's the difference between free and available i mean if somebody asked me if i'm free for dinner i'm not going to answer i'm free but i'm not available they'll probably give me a very funny look now earlier in the video i told you that the available field is the only one that you really need to pay attention to and that is true i didn't lie to you i didn't mislead you but that's also not the full story now what i'm going to do is explain the difference and in order to do that i'm going to have to confuse you and then after i confuse you i'll then correct the confusion so the confusing thing is that where you see right here i have 189 megabytes free that's actually true that's how much memory i have free right now all of the memory on the system is legitimately being used except for these 189 megabytes but wait a minute i just showed you that this field right here is actually showing you how much memory is available and that's the only field that you should pay attention to so why am i now telling you that all but 189 megabytes are actually being used at this time well nowadays it's important to understand that memory is thought of differently than it used to be if you started using computers when i started using computers or perhaps before that then you would actually consider 189 megabytes being free out of four gigabytes to be a big problem but that's just not how things are thought of anymore unused ram is wasted ram i'm going to repeat that unused ram is wasted ram and the reason why this is important is because if you have something in memory it could be retrieved faster than it could be retrieved from the disk so what a linux system tries to do is cache as much as possible or as much as what makes sense to cache so that way what you might need or what you might access can be retrieved a lot faster because it's sitting in ram rather than being pulled directly off the disk and in addition to that this concept is less of a burden to your disk because if something is being retrieved from ram and then written to in ram before it's synced to the disk then that means the disk is getting less rights overall and that's going to help us maintain that disk and see if we can make it last a lot longer because the less data that we write to the disk and then rewrite and then rewrite and then rewrite the better the overall health of the disk so what we'll do is we'll store data in cache we'll retrieve it from cache we'll write to the cache and then at some point we'll sync that cache back to the disk but what does that actually mean well under the available field we have 1040 that's how many megabytes are actually available and that amount of memory is actually truly available sure it consists of data that is actually sitting in ram basically a cache it's being used but if you launch an application and that application needs more memory then the system will actually give it some of the memory from the available field right here so it's pretty much the equivalent of being unused i mean it is being used there's a cache in play here but it's only used when it's not needed and if it is needed then the system will give some of that memory to the process that needs it so in shorter summary ram that's sitting around idle not being used is wasted so the system is trying to make good use of that memory by caching things but it's actually being a team player it's going to give that memory away whenever it's needed it's not holding it for itself it's just using it for right now there's idle memory idle memory is wasted let's use it for something good and that's exactly what it's doing so even though the memory in the available field is legitimately being used it's only being used because the system doesn't have any designated use for that memory as of yet so it just uses it for the cache to make use of the ram in absence of something better to use it for now the available field actually includes the amount from the free column as well so when i mentioned earlier that the available field is the only one that you should need to pay attention to i wasn't kidding the available field is literally the total amount of memory that processes can get access to if they need memory so yes 1040 megabytes are legitimately available for use in addition there's also cash and buffers as well and even some more complicated aspects of how memory is managed on a linux system but i'm actually going to stop my explanation of memory usage right here because what i've gone over in terms of memory usage is exactly what you should memorize and what you'll use on a daily basis let's move on to swap and discuss that in my case right here i have 511 megabytes of swap that's available that's my total available swap and of that i'm currently using zero megabytes of that which is actually a good thing we want to use as little swap as we could possibly get away with and the reason for that is because swap is slow and i mean extremely slow so slow in fact that we would rather nothing use swap if we can get away with it but what exactly is swap anyway you could think of swap as emergency memory let's say for example that you ended up using all of your four gigs of ram you have nothing free nothing unavailable nothing at all at that point the system's out of memory and well being out of memory is a really bad thing a linux server can't function without memory so if it runs out of memory things are going to come to a screeching halt but what swap does swap is actually giving a temporary amount of memory that's available for emergency use but the problem with swap is that swap exists on the disk on the storage volume and the storage volume is always slower than memory that's why we have a cache if you recall what i mentioned earlier about unused ram being wasted ram well the same concept applies things are being cached into ram because it's just that much faster but when we run out of ram then all we really have is the hard disk and swap is how much of the hard disk or the storage volume can be used for stand-in memory so basically swap is something that we usually want to have but we hope we never end up having to use it kind of like car insurance when it comes to some linux implementations we might not have any swap available to us at all one example of this is if you're running a kubernetes cluster instances within a kubernetes cluster will typically not have swap at all because it ends up working against the cluster now that situation isn't very common there's not very many apps out there that run better in the absence of swap altogether but i bring that up just to let you know that there are some situations out there where you might not want to have swap at all however that's not the only reason why you might not have swap on a particular server and another reason could be that the administrator that set up the server initially if that person isn't you might be oppositional against having swap some people out there are very adamant on this they will let you know that swap is something you shouldn't have you should run without it swap is bad it only hurts you so just don't use it but actually there's no truth to that at all and this is actually believe it or not a very common debate in fact pretty much every linux community that i visited personally has had at least one topic thus debating whether or not you should use swap at all now i'm going to end that debate right here there's no reason whatsoever unless the application such as kubernetes requires you to be in the absence of swap that you should actually well be in the absence of swap so definitely make sure that you have swap in some form or fashion on your linux server because it might just save you if your server is going to otherwise come crashing to a halt in the absence of memory but the thing is the debate when it comes to whether or not to use swap actually kind of makes sense swap is bad like i mentioned hard drives are slow we don't want to use them for memory anything that you have in swap is going to be orders of magnitude slower so yeah it's true that swap is a bad thing we hope to never use it but again we definitely want to make sure that we have it so that way if we're in a bind maybe it's going to help us out now the thing is even if there was some sort of truth when it comes to running without swap is having some kind of benefit keep in mind that storage devices are a lot cheaper nowadays than they used to be i mean you'd be surprised what a 20 ssd can actually buy you nowadays storage prices have come down a lot so in my case i see no excuse whatsoever why an administrator can't spare one or two gigabytes for swap i mean are you actually so hard up on hard drive space that you can't spare a single gigabyte for swap i highly doubt that's the case now the thing is like i mentioned we hope to never use swap but some applications will use swap a little bit so using a little bit of swap doesn't automatically mean that your memory is running out i mean if you're using a lot of swap then yeah that's probably the case but using a little bit of swap isn't actually unusual but if you have no swap whatsoever and the application you're running is actually designed to use a little bit of swap well it won't be able to because you have no swap so when it all comes down to it even though swap is something that we really wish we'd never end up using we really should have at least a little bit of swap if nothing else it might just save the day at some point in the future so how exactly does a linux server then calculate or make a determination as far as whether or not it should be using swap at any given time well what i want to do right now is go over the existence of the swappiness variable swappiness is a value that helps the system determine how much swap it should be using at any given time and what i'm going to do is switch over here to server number three because this server has had the least number of custom configurations on my end so it's going to be the most similar to a real server that you might see out in the wild anyway let's see if we can grab the current swappiness value for this particular server it would be interesting to see what that number actually is and to find it we can run sys ctl and then vm dot swappiness just like that so i'll press enter and we get a value of 60. now one thing that a lot of administrators will do is they will adjust this value to get the most performance out of their server that they can that's something that i can cover in a different video i'm not actually going to show you how to change this value in this particular video and even if i did include that into this video it probably wouldn't help you that much in the context of what i'm going over because what you want to set the swappiness value to depends on the workload or the application i can't provide you with one value to use here that works across the board because every server is unique if nothing else 60 is considered a happy medium so typically that's going to be the default value that swappiness is set to on most distributions or at least the majority of the distros that i've tried personally anyway the lower the swappiness the less likely swap is going to be used now you might assume then that if you set this to zero you're effectively disabling swap but that's not actually true with a swappiness value of zero that means that your linux server will do everything in its power to never use swap if it can get away with it it doesn't mean that it's never going to use swap it just means that it's aggressively going to try to not use swap but it's still going to use it if it absolutely has to and i mentioned earlier that swap is technically a bad thing because it's going to be fetched a lot slower than if you fetched it from ram so i'm not actually telling you to set your swappiness to zero i just wanted to give you that metric again there's no one answer here for what this value should be set to it depends on your server and what types of processes are running on it [Music] anyway that was a simple explanation of memory usage how to read the memory usage and also what swap is and why we generally don't want to use it but we still have to have it while also debating whether or not we should be using it linux is very interesting to say the least anyway in this video we took a look at memory usage also how swap factors in so hopefully this video was helpful in teaching you the basics of memory usage and how to understand how much memory is being used but keep in mind memory management linux like i mentioned earlier is a rabbit hole a very very deep rabbit hole it's a fun rabbit hole if you're into computer science but if you're not then what i've gone over in this video should probably be enough for most of you anyway let me know what your thoughts are in the comments down below if you found this video helpful please click that like button and i'll see you in the next video as soon as i have it uploaded thank you so much for checking out this content and i hope you have a great day [Music] you
Info
Channel: Learn Linux TV
Views: 27,268
Rating: undefined out of 5
Keywords: Linux, LearnLinuxTV, Learn Linux TV, LearnLinux.TV, Learn Linux, Linux Training, linux server, linux tutorial, ubuntu, debian, centos, memory usage, swap usage, memory, swap, free command, htop, linux tutorial for beginners, linux commands, linux command line tutorial, linux command line, linux essentials, linux crash course, linux swap tutorial, swap partition, linux swap space, swap partition in linux, linux for beginners, linux tutorial 2022, swap memory
Id: XTMyJ5l0GLg
Channel Id: undefined
Length: 20min 55sec (1255 seconds)
Published: Tue May 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.