Linux Talk | Linux Memory Management

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
greetings and salutations thanks for clicking on the video today we're going to talk about memory management in Linux and this video is going to give you an overview of some of the more important aspects of how a Linux computer uses the physical RAM on your system and this video is also going to give you a couple of tips as we roll roll along to monitor your memory and also a couple of things that you can do to actually take control and modify what the system is doing I did a video not too long ago on a CH top which is the program that is running here in the terminal and it created a lot of response and a lot of folks had questions and one of the questions that I got was from a fella who asked why it was that even though he had like 16 gigabytes of memory in his computer or whatever it was that as he would open and close programs the memory would still seem to be being taken up even though he had closed the program so in other words he'd started out with a certain amount of memory and opened up his game or his browser or whatever it was and then the next thing he knew he closed the browser but he didn't get all the memory back he thought something was wrong well actually there was nothing wrong and that's the first thing we're going to talk about is understanding about the buffer cache that is running on your computer all the time so if we look at H top here we're gonna zoom in and we're just going to look at these numbers up here and yes I know the CPU is running at 100% gang that is because this is the old e machine computer and we are running simple screen recorder which is encoding video in the background and that's this is normal operation for this machine also I have a bunch of stuff open right now I'm trying to suck up a lot of system memory for demonstration purposes later in the video so at this point we're using a little over a gig of memory and this machine has just under two gigs with nineteen hundred and ninety megabytes of physical RAM we're using 11 megabytes of the swap partition we're going to talk about swap later and how that works and so this is the current state of the system what I want to do is jump over here to another desktop and we're going to open up another terminal here and I'm gonna show you a command that will allow you to very quickly check and see exactly what's going on with the memory on your computer and I'm gonna show you how you can do a couple of tweaks here to make it really useful so yeah you could open up a CH top or you could use an applet on your desktop if you're really concerned about memory usage or you could open up like system monitor or something like that but you could also just jump into a terminal and you could run this command which is free and it will give you all of the information about what is going on with the system memory the only thing about this is because of the way it outputs the information it's not necessarily the easiest to just glance at and figure out so we're going to run free with the H argument which will make it humanly readable okay that makes more sense right now it says I got 1.9 gigs on this computer I'm using one point eight gigabytes total and the cache itself is one gigabyte and it also shows how much swap we're using so I've got two point eight gigabytes of swap and then it is also showing here the how much I'm using which is still 11 megabytes so anyway pretty pretty simple basic information there we can make this even more useful by adding another command to it so if we type watch free and then give it the the H little argument at the end there okay the little option you know we'll get a screen that looks like this what the watch command does is it makes any command on the system work like a meter it makes it run almost in real time and you can put options in watch that will change how often the command is run by default it's every two seconds but it could be 1.5 seconds or even point 1 seconds which makes it almost in perfectly real time or you can have it do it every 10 minutes it all depends on how you set it up the default is 2 seconds so we have freerunning here and it's just keeping an eye on the system memory and I'm gonna leave this running this way and then I'm going to open up another tab over here because I'm gonna show you another command let me get in here and like I said I got a bunch of stuff open I got a couple of browsers I've got the word processor open here okay first command we're gonna look at don't switch back there thank you and do that the first command that we're going to look at is this one right here so I'm gonna grab this and I'm gonna copy it jump over to my other desktop I'm gonna paste this in and it wants to run it I don't want to run it I just want to look at it so let me clear that out and then boom boom there's our command no I didn't want to run it I only wanted to look at it try that one more time okay first thing I'm gonna do is clear it dot it there we go okay all right so let's break this down a little bit it's a big long string here and I'm gonna show you what it does since it starts with sudo that means that we're going to run it as an administrator and if we look here this is SH which is the shell we're going to use most folks are used to bash and bash is pretty much the default shell but every Linux and UNIX computer that you ever sit down in front of is gonna have the SH shell on it it is the most universal shell and a lot of times you'll see scripts and commands that are going to be used in SH just to make them universally usable so just in case you write a bash command and bash is not on that machine for whatever reason you know that sort of thing all right so we're going to use SH and the of course the argument here is C we means we're gonna run a command so everything that's in these quotes is our command that we're going to run and basically what all this does is it is going to create a flag file in your proc directory or pross directories that is a system directory that contains files that represent all of the currently running programs and processes and services on your computer remember that in Linux and UNIX everything is represented by a file including all the devices on the machine all the running processes everything so this is how we're going to do it if we send this command to the system it's going to create this flag file in there and then the system is going to dump the cache in other words anything that it's hanging on to it's going to get rid of so first thing we're going to do is to go in here I don't know whether I mentioned it at the beginning of the video but I'm getting over a bad cold gang so excuse me if I cough we're gonna go ahead and leave the free command running and just kind of take a mental note of what that says you want to jump over to this other desktop and I'm gonna close a lot of these programs that we have open right now so let's go ahead and open up the web browser here I got Firefox running just on the front page it's not doing anything but it's taking up space so we'll go ahead and close that and we're gonna go over here and we're gonna close Google Chrome we don't need that open right let's go ahead and close that and I'm going to run over here and let's get this word processor and we'll go ahead and close that so this should free up a lot of system memory let's jump over here and see what we got going on now well now it's telling me I got one point three gigabytes used and our cache now is down to five hundred and sixty seven megabytes so let's go ahead and run this command okay let's go ahead and run this command and we will see what it does excuse me and we're gonna keep jumping to desktop so I want to jump to the other tab oh now we've we've we've reclaimed some memory back we got the memory usage down quite a bit we're down to 783 megabytes of total memory used and we freed up quite a bit we got 1.2 gigabytes free on the system so that's pretty awesome right they're still using 11 megabytes and swap so that is what that command does and I realize that that is a big chunky command right there and you're probably saying to yourself well every time I want to clear the cache I I really can't type that in well I mean clearing the cache manually is not something that you ordinarily need to do the memory manager in the system does a good job of keeping up with it things that sit in the cache for a very long time they get dumped out automatically if they're not called on and if the system does run into a memory crunch it starts dumping the cache anyway but you may have a situation where you want to you're just real anal about it and you want to keep that clear you can take this command and then you can put it into a script and then you can take that script and you can put it in cron which is a program that runs on every Linux system that keeps up with commands that you want to run on a regular basis you can put all kinds of things in cron you can put like command to back the system up at 3 o'clock in the morning when everybody's asleep or something like that well you could put this in there and you could tell it to do it like once every couple hours if you wanted to that might be very useful if you have a low memory system and you're running like a media server on it or some sort of a server something to keep in mind so there you go all right so let's talk for a minute or two here about swap I talked about this in a past video a Q&A video but I'm gonna go into it in some detail here because it's the other aspect of memory that you can control currently on this system we're using 11 megabytes of swap and that's because I had all of that stuff open right and what is swap how does it work what does it do well swap is a area on your hard drive that you set aside for the system to use as virtual memory back in the days when we had computers that had very low memory and memory was very expensive to make the computer be able to do more without running out of memory and crashing or putting up an error message that told you to go ahead and close something because the system was out of memory they started using swaps swap files I think in Windows nowadays they call it a page file it's done pretty much in every system and what it basically does is things that are in the memory that the memory manager determines that are not high priority than other words they're not being called on right now it can actually take that page of memory and then it can throw it over on the hard drive and then if it needs to get it back it can read it back into memory from the hard drive and that's pretty cool if your system starts running out of physical memory then your stuff doesn't stop working it just starts using the hard drive the downside to this is that it's very slow using the the swap file on your hard drive as memory is very slow because hard drives even solid-state drives are slower than the RAM that's in your computer with solid-state drives it's very important that you kind of manage how swap works because swap in by its very nature will write and read and write back and forth a lot because the system might throw something into swap and then get it right back out and it might be doing a lot of reads and writes there and with solid-state drives we want to try and keep the write cycles down to a minimum because the more you write to a solid-state drive the quicker it will wear out spinning drives of course you know reading and writing wears them out over time as well but it's a little bit of a different concern with an SSD so for whatever reason you may want to be able to manage how the system does swap and I'm going to show you how to do that so the next command we're going to take a look at it's another long command here is this one so let's come back over to our other terminal we have open here clear it and then I'm going to paste this in and we're gonna break this command down and talk a little bit about what it does this is a one line command that adds a line of text to a file on your computer instead of having to open up a text editor and scrolling down to the bottom and adding the line this line will do it and here's how it works first of all we're doing it as administrator because we're going to be writing to a system configuration file and we're gonna use bash this so this will be a bash command and you could use Sh I suppose but I don't know whether the syntax lines up so let's just stick with bash alright and then we have you know tell it to run a command and then just like the one before we're going to echo everything in the single quotes here is going to be the line that we're going to add to our file and then here where we have two arrows that tells the system that we want to add that line to the end of the file we don't want to replace everything in the file with this text right here if you only put one arrow in here it will actually overwrite the SIS CTL configuration file completely so be careful you don't want to do that so instead of running this command like this let's do this let's go in and do this manually so you can get an idea of what we're doing alright but if you just want to run this command to change the swap enos on your system then you can do it and you can change this number right here and we'll talk a bit more about that in just a second so let me go ahead and get out of here so I can run my next command to show you what's going on so we'll do sudo nano et Cie this CTL dot conf make sure I do it in there alright so we've loaded up this file and this is basically a configuration file for the kernel itself it sets parameters in there so do be careful as you poke around because yes you could trash your system real easy messing around with this sort of stuff same thing with sending flag files to the process directory as well that might that's a good way to crash a system okay so we just want to add our line of text down at the bottom so I'm going to go down here and you'll see below that last quote let's go ahead and just add an extra space just for for measure good measure here it ignores blank lines when it's parsing the file when the system loads up and we're gonna put in VM swappi nests and we can say equals and what does this number mean that we're putting in here so let's talk about that before we type anything now this depends somewhat on the distribution of Linux you're using but generally speaking when you install Linux especially I do know this is true for a boon to and debian and anything based on a boon to the swap enos by default is set to around 60 which kind of a rough way to understand that is that the system will actually have to use sixty percent of the memory in the RAM before it starts swapping now there are many other factors that go into that it's not just the memory usage so that's a very general way of looking at that but it's a good thing to you know good way to remember what we're talking about here okay let's put it that way so if you have your system set to the swapping is set to 60 and you use memory intensive programs or you have low memory that means the system is going to start swapping as soon as it starts to the memory gets just a little bit over half full you can improve the performance of the system a little bit by lowering that in this case I'm gonna put 15 in there which kind of sort of means that 85% of the system memory needs to be used before the system will start to swap this means it will swap less ok if you are using an SSD drive and you had a swap file or a swap partition on that drive then you would set this to 10 which would basically mean that the system would hardly ever swap at all but 15 is we're just gonna put that in for a performance deal here today excuse me now there is some debate and discussion I talked about this in my video that I did my Q&A video but I'm gonna mention it again here and that is some people say well if I have a lot of memory on my system like if I have I don't know more than 8 gigabytes of memory do I need to have a swap partition at all the answer to that is yes because there's several reasons for that by default when you install Linux it will create a swap partition on the on the drive that is just a little bit bigger than the amount of RAM you have in the system that is so if the system crashes at any time that everything in the memory will be dumped to swap and that way it can be debug now if that's something you don't care about and there's not many people on the planet and actually know how to do it anyway excuse me then what you can do is you can just make the swap file a little bit smaller if you don't intend to hibernate your system you can make that swap file smaller but to completely eliminate the file is not a good idea because there are certain programs and processes that actually depend on it be there so what I find is is that if you run a system without swap even if you have a lot of memory in it the system may become unstable and since sleep realized sometimes on the on the swap in other words when the computer suspends it may write some of the more critical stuff to the swap file it really needs to be there so if you have a great big bout of memory in the computer you can make that swap file small or you can make it two gigabytes but don't make it go completely away you can also eliminate having a separate swap partition and have a swap file like Windows does Windows creates a file called page file cysts the only problem in Windows is that file is usually dynamically allocated by default and so it can grow and get very big and it can fragment and do all kinds of crazy things there are tweaks that you can do in Windows to limit that and in Linux you can create a swap file and you can let it inflate and deflate as needed or you can have it set to a particular size but by default you usually set aside a little bit of space on the partition and switching over to using a swap file is something that you would do in special cases so this is all plumbing so now that we have our command in here I'm going to go ahead and save this alright and the next time I reboot the system it will lower the swap enos and we can go ahead and exit that and we go ahead and get out of this as well if you have a command running in watch then you can just ctrl C to close that command so there you go gang it's a little bit about what's happening with the memory on your system and some of the things that you can control excuse me in most cases the memory management of your computer is going to take care of it itself Linux has great memory management it changes from kernel version to kernel version they tweak it up every now and again so it might act a little bit different so if you're running an old kernel and then you went to a new kernel you might notice that a little bit more memory a little bit less is used it's just it's just the way the kernel is the newer kernels are going they're trying to find more efficient ways to use RAM on computers and it usually takes care of itself it's not something that you have to mess with and but the idea of this video was just to give you a better understanding of what was really going on and I hope I've done that now if you would like more information about Linux and you want to learn some more I highly recommend that you check out freedom penguin comm because there's a lot of great articles up there contributed by cool folks such as myself and always something interesting to read so that's one thing that you can do another thing that you can do is go check out easy Linux comm and that is where I explain how I can help you get started with Linux and give you some personalized IT support do check that out contact me if that's something that you're interested in check out easy Linux on Facebook easy Linux is becoming quite a little Facebook community I try and post things there that the average Linux user desktop user would be interested in I avoid these super nerdy stuff most of the time so you might want to check that out as well and if you do check out easy Linux on Facebook give it a like please I would really appreciate that and thank you for watching this video these commands will be available in the description for the video so if I can get them in there I may have to abbreviate them there was one command that I tried to post that it wouldn't take so I will see what happens if I can get them in the description you'll see them if not they're gonna be right here these longer commands tend to be hard to cut and paste in there because there are certain things that certain characters and whatnot that YouTube doesn't want in there so anyway gang thank you for watching we will talk again soon bye bye
Info
Channel: Joe Collins
Views: 41,135
Rating: undefined out of 5
Keywords: Linux, Computer, Memory Management, Operating System, tutorial
Id: WeujdYCRrYM
Channel Id: undefined
Length: 23min 40sec (1420 seconds)
Published: Fri Jan 29 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.