In GNU/Linux, the
free command displays information about the memory usage of the operating system such as the total amount of RAM available to the system, how much of it is free and how much is used by the operating system & user applications etc.
There are many other commands in GNU/Linux that can be used to view such information, but the
free command gives a clean & a simple overview of the system’s memory that is easy to ‘read’ (well, when you use it properly). In this article, I’ll give you some simple examples of various ways you can use it to gather useful information about your system.
But to first answer the question (one of my friends who’s learning GNU/Linux asked me about it), when you use the
free command with the
-g argument, it simply displays the memory related information in Gigabytes. Below screenshot shows what I got when I ran it in Ubuntu 15.10.
As you can see, it’s difficult to understand what those various numbers indicate (I’ll explain them in a minute). But, I think it’s reasonable to assume that anyone can intuitively guess what’s displayed under the field ‘free’. Yes it’s the free amount of memory available on your system, and in my case, it’s 0!. Now of course, every time you enter this command (with the
-g option) it won’t say zero, but if it does it can be quite daunting to a new user (which is also what my friend got intrigued by).
Making it more human readable
So, in an attempt to further understand what’s going on, the easiest thing we can do here is to make
free give us a more human readable output, something that is also accurate. We can do that by simply adding the
-h option. So instead of
free -g, if I enter
free -gh, it’ll give me an output similar to the below one.
As you can see, as soon as we use the
-h argument (option) the
free command becomes smart in the sense that where applicable it’ll display memory usage data in Gigabytes, but when it doesn’t make a lot of sense to do that, it’ll use the metric that makes the most sense. For instance, under buffers it says 72M (Megabytes) rather than 0.072G (Gigabytes). And now under ‘free’, it says 422M, not zero.
[ms_alert icon=”fa-hand-paper-o” background_color=”#f5f5f5″ text_color=”” border_width=”0px” border_radius=”0px” box_shadow=”no” dismissable=”no” class=”” id=””]What had happened earlier was that we had forced
free to use Gigabyte as the metric thus small values simply got discarded and ended up being labelled as zeros.[/ms_alert]
If we just run the command only with the
-h option, then the command will decide its own which metric(s) to use.
What do those various fields mean?
Before I answer this question, let me give you a quick and easy to understand overview about the cached memory. Without this basic understanding, you’ll get confused by some of the data, such as the amount of used and free memory usages, indicated by the
What is cached memory?
When you open a video file (as an example) on your computer, have you noticed that the first time is the slowest when opening that file? And that after closing it, if you re-opened it again (say within a short while), then it opens up much faster. This is because the operating system has kept it in the memory, even after you closed it, simply because it can re-open it pretty fast, if it’s already found in memory (since RAM is very fast compared to other permanent storage devices).
In GNU/Linux, the area where these type of data is hold is called the Page Cache, and by default the operating system allocates all the free space of RAM to the Page Cache. And the operating system basically treats the Page Cache as free space. So for instance, if a user tried to open a program and there was no enough space left on RAM to load it, then the OS will wipe out a portion from the Page Cache to make way for that particular program.
So with this simple understanding in mind, let’s move on to the next section.
total -- This is the total amount of memory (RAM) available to the operating system. In most cases, this indicates the actual physical size of your RAM (unless you’ve changed how much should be available to the OS. You can do this is GNU/Linux by tweaking the boot-loader’s configuration file).
used -- This is the total amount of RAM that is used by the system (this is basically all there is loaded on the RAM, including the Page Cache of course). And this is where usually people get confused. Because sometimes when you compare the ‘used’ memory indicated by the
free command with the ‘used’ value indicated by a system monitor (either a GUI or command-line one such as
htop) the values can be quite different. And the reason simply lies in how each program has chosen to define the ‘used’ memory.
Some system monitors include the Page Cache when calculating the total memory usage. Others simply ignore the Page Cache (again because the OS doesn’t take it too seriously and wipes its content when running out of memory) and the Buffer Cache (explained below), or pretty much any kind of data caches.
free -- This is the total amount of free RAM (calculated by: total
shared -- This is the total amount of memory currently used to share data between multiple programs.
buffers (aka Buffer Cache) -- This is the total amount of memory used by the OS to bring data in from other hardware devices (mouse, keyboard, storage media such as SSDs, HDDs, CD/DVD ROMS etc) to the memory. It’s also used to pass the data out of the memory to the appropriate device as well (for saving a file from RAM to the disk for instance). While this is also a type of a data caching mechanism and part of its data is kept in the Page Cache, a portion of the Buffer Cache is kept separated from the Page Cache (again, I’m skipping some technical explanations here, you can however, find a great explanation from here) and that’s what represented here.
cached -- This is the total size of the Page Cache.
Under -/+ buffers/cache:
used -- This is the total number of memory used by the OS & the user applications (‘shared’ memory is also included here), excluding the Page Cache & the buffers (Buffer Cache).
free -- This is the total number of free memory available, again calculated (unlike under ‘Mem:’), by totally disregarding the Page Cache & the buffers (Buffer Cache). In fact, this is the ‘free’ memory amount that is indicated by most of the system monitors that I explained above (this is also what is indicated as the free memory by ‘htop’ and which is also why there was an exact match between the ‘used’ memory indicated by ‘htop’ and ‘free’ on the image above this one. This is a different screenshot though, so don’t compare the numbers of the two).
RAM is fast, yes, but it’s very expensive, therefore its size is limited. And there can occur situations where even after wiping out the whole Page Cache, the operating system is still unable to find any free memory to run new programs. This will make the program that you’re trying to run crash or in severe situations, even the whole system to crash.
That is where Swap comes into play. In GNU/Linux, it can either be a separate partition or simply a file the operating system uses (as a virtual memory) to temporarily move data from the RAM for clearing up some memory. But since the main storage units are quite slow compared to RAM, under severe conditions, it can be extremely frustrating for the user and consequently the OS could easily loose its responsiveness. Basically, the lower the Swap usage the better. And it’s the information of the Swap (file or partition) that
free displays under ‘Swap:’ The total size, how much of is used & how much is free. Here’s it’s empty because I’ve not setup a Swap file.
What are the other options?
This will force
free to use Bytes when showing the memory information.
You’ll get an output in Kilobytes
Yes you guessed right, this will give an output in Megabytes
free -s 1
When invoked in this manner,
free will output the data on each second (replace 1 with your preferred interval) on to the terminal. But as you can see, the output doesn’t look that pretty (it’s better run it with the help of the ‘
watch‘ command. I’ll talk about it in another lesson, but here’s the command anyway:
watch -d -n 1 free -h)
-t option you’ll get a sum of all the values under each column. There are a couple of others options comes with
free, you can read about them from its manual. Use the below command to read it.
So hopefully this article gave you a reasonable understanding of the GNU/Linux
free command. Good luck & if you have questions, please do comment, I’ll do my best to resolve them.
5 thoughts on “‘Linux’ free -g Command Output (& Other Examples)”
Thanks for the good info!
You’re welcome 🙂 .
free -s 1 or any other number isn’t working
free: seconds argument `1′ failed
Also I don’t see the cached line either
total used free shared buff/cache available
Mem: 7.7G 848M 5.8G 206M 1.1G 6.6G
Swap: 0B 0B 0B
the ‘free -gh’ is working just fine on Ubuntu GNOME 16.04 LTS, but surprisingly, the ‘free -s 1’ does’t work on it! I wrote this article on Ubuntu 15.10 and these commands (options) work on it (after all, these options are listed in its manual). I’m not sure, but it’s probably a bug.
But you can always use the ‘watch’ command to get an automatic update on the memory usage on each second. For that you can use the below command:
watch --interval 1 'free -m'
To learn more about the ‘watch’ command, please refer to its manual using the below command:
To make it update every x seconds and also make it user friendly, this works:
free -s 30 -gh
This will update every 30 seconds, and show values in a user friendly way, in GB (or MB where appropriate).
Alternatives are using the watch command as mentioned in the article, or:
while sleep 30; do free -gh; done
,which can be run in any Bourne shell supposedly.