Dec 122014
 

There’s nothing more crucial to a computer or server than memory.

Information on memory installed, memory used and memory free are indispensable data to any Linux system administrator.

Should memory on your system be inadequate for the load, the results will be most unpleasant to users.

The best way to understand memory use and availability on your Linux system is via the ‘free‘ command, a feature that comes with most Linux installations.

Understanding Memory

Before jumping into the nuances of the “free” command, let’s spend a few seconds to understand the basic memory concepts.
* Physical Memory – Amount of memory installed and currently used
* Real Memory – How much memory is used by the applications
* Swap Memory – Portion of virtual memory on hard disk that’s used when RAM is full (high swap memory usage is not good)
* Virtual Memory – Combination of RAM and disk space that running processes can leverage

Free – Basics

On the command line, we use free to get information about memory use on a Linux desktop or server.

In this post, let’s consider the various options for free.

usage: free [-b|-k|-m|-g] [-l] [-o] [-t] [-s delay] [-c count] [-V]
-b,-k,-m,-g show output in bytes, KB, MB, or GB
-l show detailed low and high memory statistics
-o use old format (no -/+buffers/cache line)
-t display total for RAM + swap
-s update every [delay] seconds
-c update [count] times
-V display version information and exit

Free – Various Commands

Let’s start with the basic command.

jason@ChristyPC ~ $ free
             total       used       free     shared    buffers     cached
Mem:       3968848    3352196     616652     200544     225512    2040400
-/+ buffers/cache:    1086284    2882564
Swap:      4022268       2016    4020252

In the above example from my Linux Mint desktop, the three lines represent physical memory, real memory and swap memory respectively.

The “used” memory in the first line is misleading because Linux systems take into account even buffer and cache.

So when you run the free command, the second line representing real memory is the most important line in understanding memory use on your computer.

In the above example, there’s still 2.88GB of free memory available, which means I’m in a comfortable zone on my Linux Mint desktop.

Display Free Output in Human Understandable Format

Sometimes command line outputs are needlessly complicated.

Thankfully, there are simple ways out.

jason@ChristyPC ~ $ free -h
             total       used       free     shared    buffers     cached
Mem:          3.8G       3.4G       387M       217M       219M       2.0G
-/+ buffers/cache:       1.1G       2.6G
Swap:         3.8G        68K       3.8G

The above command worked fine on Ubuntu and Linux Mint and by extension should work on all Debian systems.

Note: Unfortunately, on Red Hat Linux systems the free -h command does not work.

I got an an error message (see below) when I ran the free -h command on a Red Hat system:

jason@ChristyPC ~ $ free -h
free: invalid option -- h
Display Memory in Gigabytes

You have the option to display in gigabytes, megabytes etc.

Let’s look at the command for free in gigabyte format since it’s easy to understand at just a glance.

jason@ChristyPC ~ $ free -g
             total       used       free     shared    buffers     cached
Mem:             3          3          0          0          0          1
-/+ buffers/cache:          1          2
Swap:            3          0          3

Display Memory in Megabytes

The below command will output memory in megabytes format.

jason@ChristyPC ~ $ free -m
             total       used       free     shared    buffers     cached
Mem:          3875       3269        606        196        220       1993
-/+ buffers/cache:       1055       2820
Swap:         3927          1       3926
Display Memory in Kilobytes

I doubt many people still look at memory in kilobytes any more since gigabyte is more the norm.

jason@ChristyPC ~ $ free -k
             total       used       free     shared    buffers     cached
Mem:       3968848    3344556     624292     201112     226092    2041088
-/+ buffers/cache:    1077376    2891472
Swap:      4022268       2016    4020252
Display Memory in Bytes

I’d be surprised if even one in a thousand Linux users look at memory in bytes any more.

jason@ChristyPC ~ $ free -b
             total       used       free     shared    buffers     cached
Mem:    4064100352 3429953536  634146816  202133504  231030784 2086174720
-/+ buffers/cache: 1112748032 2951352320
Swap:   4118802432    2064384 4116738048
Display Total Line

The below command combines physical memory and swap memory to give you total memory.

jason@ChristyPC ~ $ free -t
             total       used       free     shared    buffers     cached
Mem:       3968848    3346840     622008     200360     226304    2040372
-/+ buffers/cache:    1080164    2888684
Swap:      4022268       2016    4020252
Total:     7991116    3348856    4642260
Remove Display of Buffer Line

The below ‘free’ command gets rid of the buffer/cache line.

jason@ChristyPC ~ $ free -o
             total       used       free     shared    buffers     cached
Mem:       3968848    3353580     615268     205088     226360    2045116
Swap:      4022268       2016    4020252
Display Memory Status for Regular Intervals

In the below ‘free’ command, I’m checking memory use every five seconds (hence -s 5).

Since I waited for 15 seconds, you’ll see three results below.

jason@ChristyPC ~ $ free -s 5
             total       used       free     shared    buffers     cached
Mem:       3968848    3347544     621304     201800     226424    2041840
-/+ buffers/cache:    1079280    2889568
Swap:      4022268       2016    4020252

             total       used       free     shared    buffers     cached
Mem:       3968848    3345948     622900     199856     226432    2039896
-/+ buffers/cache:    1079620    2889228
Swap:      4022268       2016    4020252

             total       used       free     shared    buffers     cached
Mem:       3968848    3346072     622776     199876     226440    2039920
-/+ buffers/cache:    1079712    2889136
Swap:      4022268       2016    4020252
Display Low and High Memory Statistics
jason@ChristyPC ~ $ free -g
             total       used       free     shared    buffers     cached
Mem:       3968848    3349400     619448     200772     226548    2040840
Low:        855884     671920     183964
High:      3112964    2677480     435484
-/+ buffers/cache:    1082012    2886836
Swap:      4022268       2016    4020252
Check Version of Free on your computer

I first ran the free -V below command on my Linux Mint desktop:

jason@ChristyPC ~ $ free -V
free from procps-ng 3.3.9

Then I ran the same command on a Red Hat server:

jason@ChristyPC ~ $ free -V
procps version 3.2.7

The above output shows it’s time to update “free” on the Red Hat system.

Free – Bottom Line

I cannot overemphasize how important memory is to the smooth running of your computer and ultimately your business, non-profit, web site, blog etc.

Folks, I hope the above ‘free‘ cheat sheet proves helpful to you as you newbies go about mastering the command line.

Now go ahead and try these “free” commands on your Linux computer.

 Posted by at 12:02 pm

Sorry, the comment form is closed at this time.