Checking available memory

Linux systems handle memory exceptionally well, though it's always possible for things to get out of hand if a process misbehaves or not enough memory was allocated. In such a situation where a system starts to perform sluggish, checking your available memory will probably be one of the first things you look into. To do this, we use the free command. To make the output even more readable, you can add the -m option, which shows your memory usage in terms of megabytes, which can make it much easier to read. Reading this output may be confusing at first, though I'm sure you'll find it straightforward after we go through the output.

Checking available memory

The output of the free command

When running the free command, we're presented with three rows and six columns of information. The first row shows us our actual RAM usage, while the second row declares buffers and the third our swap usage. Under total, we see that this system has 7923 MB of RAM installed. Technically, this system has 8 GB of RAM, though some is reserved for the kernel or some kind of hardware and may not show here. In the next column (used) we see how much of our system's RAM has been consumed, followed by free where it shows us how much of the system's RAM is unused. In our preceding example, it would appear as though we only have 927 MB free of our 8 GB, but that's not exactly correct. So, how exactly does one interpret how much memory is actually free?

First, used on the first line corresponds to how much memory is actually being used, including what has been cached. Essentially, memory management in Linux declares what is known as a disk cache, which is a chunk of memory set aside for data that has yet to be written to disk. You can see this in our output of the free -m command; it's the number on the far right underneath cached. This memory is not necessarily being used by a process; it's declared in order to make your system run faster. If a process is started and it requires more RAM than what shows in the first line under free, the Linux kernel will happily give up memory from the disk cache to other processes as needed.

The disk cache helps increase performance. When you read something from the disk, it is stored in the disk cache, and then read from there instead of from the disk each time. For example, say you take a look at a text file saved in your /home directory several times each day. The first time you read it, you're reading it from the disk. From that point on, it's stored in disk cache, and accessed from there each time you wish to read the file from that point forward. Since RAM is faster than your disk, this file will open each additional time because it only has to read it from disk one time, then going forward its read from the disk cache.

The information stored within the disk cache ages out over time. As disk cache fills up, the oldest information stored there drops off to make room for other things. In addition, when memory is needed for processes, memory from the cache can be taken back at any time. This is why that even though it may appear that an excessive amount of RAM is being used up by the cache at times, it's not a big issue—applications are never prevented access to this memory when they need it.

Going back to our example, the number we want to look at when determining how much memory we have free is the amount shown in the second column, on the second row. In the case of this example, 3736 MB is considered free. This is plenty of free memory in regards to this particular system. You should worry when this number decreases and swap starts to increase to compensate. As long as your system has enough RAM for its designated purpose, swap should barely be used. A small amount will almost always be used, but it is a problem when a large amount is being used. When your system actually does start to run out of memory, it will start to use your swap partition. Since your hard drive is many times slower than your RAM, you do not want this. If you see your swap space being abused, you should run some sort of resource monitor (a few of which we discuss in this chapter) to identify what is using it up.

To make sure we have a well-rounded understanding of the free command output, let's go over all of the sections it contains, starting with the very first row. We already covered total, which is the amount of memory your system has physically installed (minus whatever your kernel or hardware has reserved). Next in the first row, we have used, which refers to the amount of memory which is being used by anything at all, including the cache. The free column is the exact opposite and refers to memory that is not being used by anything whatsoever.

The last two items on the first row are buffers and cache. While these two sections aren't being used by any process, the kernel uses them to cache data for performance optimization. But if a process needs more memory, it's welcome to take from these two numbers. We already covered the disk cache, which is the last number. The buffers refer to data that hasn't yet been written to disk. Linux will, at various intervals, run a sync to write this information to the disk. You can even run the sync command yourself if you want, though this is rarely necessary. The concept of a buffer is also a key indicator on why you don't want to abruptly remove external media from your computer without unmounting first. If your system hasn't yet synced the data to the disk, you may lose it if you eject the media prematurely.

On the second row, we have -/+ buffers cache (which in our example above is 4186 MB and 3736 MB, respectively). The first number on this row (4186 MB) is a number calculated by subtracting the total of buffers and cache (2808 MB) from the used column of the first row (6995 MB). This gives us a total of 4187 MB, which is a bit off due to rounding (we're viewing the output in MB since we used the -m flag, so we're off by a small amount), but close enough. If we followed the same math but without the -m flag in our free command, the result would've been exact. The next number on the second row is 3736 MB. As mentioned earlier, this is the amount of memory that is actually free for the system to use. To get this number, we subtract the used memory (4186 MB) from our total memory (7923 MB).

Again, the amount of memory under free on the second row is the number you care about when wondering how much memory you have left. However, it's also important to understand how we arrived at this number and how Linux manages memory for us.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset