Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

free and top are built-in Linux commands used to get detailed reports on a system’s memory usage. These commands are often misinterpreted by users because neither is very clear about what it actually measures.

In this tutorial, we’ll discuss these commands and look at some of the similarities and differences between them.

2. free

free is a popular and powerful Unix command that gives information about memory usage in a human-readable format. It shows the total amount of free and used memory on the system. It also includes physical space, swap spaces, buffers, and caches used by the kernel. By default, it displays memory values in kilobytes(kb).

The information is retrieved by parsing the /proc/meminfo file. We can view it using the cat command:

$ cat /proc/meminfo
MemTotal:        8007956 kB
MemFree:          195700 kB
MemAvailable:    2721768 kB
Buffers:          420764 kB
...truncated...

The free command has this basic syntax:

$ free [options]

It also has a lot of options that we can use to manipulate the display of data. Let’s use the -h to display the output in human-readable format:

$ free -h
              total        used        free      shared  buff/cache   available
Mem:          7.6Gi       4.3Gi       264Mi       623Mi       3.1Gi       2.5Gi
Swap:         2.0Gi       6.0Mi       2.0Gi

Here’s what each of these columns represents:

  • total: refers to the total amount of memory available for use by running applications and services
  • used: shows the amount of used memory. It’s calculated as used = total – free – buffers – cache
  • free: shows the amount of unused memory
  • shared: shows memory used mostly by tmpfs
  • buff/cache: shows the combined memory usage from kernel buffers, page cache, and slabs
  • available: shows an estimate of the amount of memory available for starting new applications without swapping

We can also continuously display memory information on the terminal by using the -s (–seconds) option:

$ free -s 2
              total        used        free      shared  buff/cache   available
Mem:        8007956     4711664      224108      638984     3072184     2367976
Swap:       2097148       13324     2083824

              total        used        free      shared  buff/cache   available
Mem:        8007956     4720488      219068      635188     3068400     2362948
Swap:       2097148       13324     2083824

The command above displays memory usage information every 2 seconds. We can notice a difference in some of the values between the two outputs because the values update regularly.

3. top

The top (table of processes) command allows us to monitor in real-time the number of processes running and kernel-managed tasks on a Linux machine. It also offers us a system information summary that displays resource usage, inclusive of CPU and memory usage.

Its interface is split into two parts. The top part displays the stats values, while the lower part displays the list of the running processes. It also supports color, highlighting, and even elementary graphs. Furthermore, it’s interactive, which allows us to browse through the list of processes and even kill a process.

There are a lot of versions of the top command. Let’s view our current version:

$ top -v
  procps-ng 3.3.16
Usage:
  top -hv | -bcEHiOSs1 -d secs -n max -u|U user -p pid(s) -o field -w [cols]

In this tutorial, we’ll be working with v3.3.16 which comes with the procps-ng package.

By default, the top command produces this output:

$ top
...
%Cpu(s):  7.2 us,  2.3 sy,  0.0 ni, 90.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7820.3 total,    239.9 free,   6660.3 used,    920.0 buff/cache
MiB Swap:   2048.0 total,   1571.5 free,    476.5 used.    450.8 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 
2832 user 20 0 4285156 194344 34556 S 6.0 2.4 22:29.39 gnome-sh+ 
...

We can even display more columns. To do this, we need to press the letter on the top command’s dashboard:

...
    Navigate with Up/Dn, Right selects for move then <Enter> or Left commits,
   'd' or <Space> toggles display, 's' sets sort.  Use 'q' or <Esc> to end!

* PID     = Process Id             GID     = Group Id               SUPGIDS = Supp Groups IDs        RSlk    = RES Locked (KiB)    
* USER    = Effective User Name    GROUP   = Group Name             SUPGRPS = Supp Groups Names      RSsh    = RES Shared (KiB)    
* PR      = Priority               PGRP    = Process Group Id       TGID    = Thread Group Id        CGNAME  = Control Group name
...

Next, let’s navigate to CODE, DATA, and SWAP columns, activate them with the space button and then press the Esc button to return to the main dashboard.

We should have this output:

...
    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND     SWAP   CODE    DATA 
  15493 user    20   0   32.4g 224864  88356 S  51.3   2.8  48:48.97 chrome         0 176216  332228 
   5346 user    20   0   28.4g 338256  95904 S  46.4   4.2  36:07.86 chrome         0 176216  521552
...

Let’s understand more about what most of these columns represents:

  • %MEM: shows the percentage use of the total physical memory by a process
  • VIRT: show total memory that a process has access to, including shared memory, swapped pages, and mapped pages
  • RES: shows the total physical memory used, including private and shared by a process
  • SHR: shows total physical shared memory used by a process
  • DATA: shows the total private memory used by a process, both physical and virtual
  • CODE: shows the total physical memory utilized to load applications
  • SWAP: shows the total amount of swap memory available

4. Comparing the Output of free and top Commands

Let’s compare the results of the free and top commands and see some of the similarities and differences that exist between them.

First, let’s display memory usage information using free in megabytes (MB):

$ free -m
              total        used        free      shared  buff/cache   available
Mem:           7820        5887         890         513        1042        1151
Swap:          2047         704        1343

Next, let’s display the information from the top command. Display of the values is in megabytes by default:

$ top
top - 15:57:32 up  9:17,  1 user,  load average: 2.40, 2.07, 1.63
Tasks: 401 total,   1 running, 400 sleeping,   0 stopped,   0 zombie
%Cpu(s): 14.5 us,  1.9 sy,  0.0 ni, 83.5 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7820.3 total,    817.9 free,   5957.3 used,   1045.1 buff/cache
MiB Swap:   2048.0 total,   1343.5 free,    704.5 used.   1092.0 avail Mem
...

The first thing we can notice is that both commands have almost similar values on:

  • the total amount of memory available, used, and free
  • the total amount of swap memory available, used, and free
  • buff/cache memory size

The slight difference in some of the values is because of the time delay between running the two commands.

We can also notice that the total memory is 7820 MB, with 5887 MB used and 890MB free. However, that doesn’t mean that running applications can only request the 890MB  free memory. The 1042MB allocated for buffers and cache can be freed up to yield more memory for new application requests.

Linux tries to use RAM to optimize disk operations by making use of available memory for creating buffers and cache. This reduces I/O operations and helps the system run faster.

5. Conclusion

In this article, we’ve discussed the top and free commands that are used to retrieve reports on a system’s memory usage. We also looked at some of the differences and similarities that exist between these commands and their respective outputs.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!