Quick Links

The Linux free command displays how much of your computer's memory is in use and how much is still available for programs to use. Its output can be confusing to the uninitiated, but we'll show you how to understand it.

The free Command

The free command prints a quick summary of memory usage in a terminal window. it doesn't have many options or tricks up its sleeves, and it doesn't take much time or effort to learn how to use it. To learn to correctly interpret the information it provides, however, is another story. It is too easy to become confused by what free is telling you.

Partly this is due to terminology---like the difference between "free" and "available"---and partly it is due to the inner workings of the memory and file system management routines of the Linux kernel. If you've got spare memory that the kernel can make good use of, it will borrow it for its own purposes. Until you need it back.

We're going to take a dive into the underlying mechanisms and data routines so that you can appreciate what's going on beneath the hood, and how all of it affects the use of your random access memory (RAM).

The free Columns

Let's fire up free with no options and see what we get:

free

the free command in a terminal window

That's wrapped around in an ugly way. On your computer, you'll be able to stretch the terminal window. Here's the output in a neater table:

 total used free shared buff/cache available

Mem: 2038576 670716 327956 14296 1039904 1187160

Swap: 1557568 769096 788472

The figures are given in kibibytes, which are 1024 bytes. On Manjaro, the free command is aliased as free -m. This forces free to use mebibytes, which are 1,048,576 bytes. On other distributions, the default is kibibytes.

The top line reports on system memory, the bottom line reports on swap space. We'll introduce the columns here, then look at them in more detail shortly. The columns for the memory line are as follows:

  • Total: The total amount of physical RAM installed in your computer.
  • Used: This is calculated by Total-(Free+Buffers+Cache).
  • Free: The amount of unused memory. Why doesn't Total=Used+Free? We'll explain that shortly.
  • Shared: Memory that is used by the tmpfs file system.
  • Buff/cache: Memory used for buffers and cache.
  • Available: This is an estimation of the memory that is available to service memory requests from applications, any other functioning software within your computer, such as your graphical desktop environment and Linux commands.

For the swap line, the columns are:

  • Total: The size of the swap partition or swap file.
  • Used: The amount of swap space in use.
  • Free: The remaining (unused) swap space

The wide Display

To separate the Buff/cache figures into their own columns, use the -w (wide) option:

free -w

free -w in a terminal window

This is the result. Instead of a Buff/cache column, we get a Buffers column and a Cache column. Here are the figures in a table:

 total used free shared buffers cache available

Mem: 2038576 683724 265708 14660 94568 994596 1160420

Swap: 1557568 761416 796152

Let's see what the figures in the columns represent.

The Total Column

This is the simple one. It's how much RAM you have installed on your motherboard. This is the precious resource that all of the running processes are fighting over. At least they would be fighting if the kernel wasn't refereeing.

Incidentally, the place where free gathers its information from is the /proc/meminfo pseudo-file. You can take a look into this file yourself with the following command:

less /proc/meminfo

less /proc/meminfo in a terminal window

The output is a single list of names and values.

Output from the less /proc/meminfo command in a terminal window

The Used Column

This is where it starts to get interesting.

The Used figure represents what you'd probably expect it to, plus a bunch of other stuff. This is the memory that is allocated to processes, taken by user programs, and used by things like the GNOME or KDE desktop environments. No surprises there. But it also includes the Buffers and Cache figures.

RAM that isn't being used for something is wasted RAM. The kernel uses spare RAM to hold caches and buffers that allow it to operate more efficiently. So this RAM is being used for something by the kernel, but not by anything in user space.

If a request for memory is received that can only be serviced by relinquishing some of the RAM the kernel is using for its own devices, then that is what happens, seamlessly. Freeing up this RAM and using it for other applications won't impact the correct operation of your Linux system---nothing's going to break---but it might impact the performance of the system.

So this column really means "all RAM that is in use by something, even if it can be reclaimed instantly."

The Free Column

This column holds the figure for the amount of RAM that is not being used by anything. Because the Used column contains the Buffers and Cache figures, it's not uncommon for perfectly functioning Linux systems to have very little RAM listed as "free."

That's not necessarily a bad thing, and almost certainly means you've got a perfectly normally functioning system that is regulating the use of RAM correctly. That is, the RAM is being used by applications and other user space processes and by the kernel in its efforts to make the performance of your computer as good as can be.

The Shared Column

The figure in the Shared column represents memory devoted to holding  tmpfs RAM-based file systems. These are file systems that are created in memory to facilitate the efficient functioning of the operating system. To see which tmpfs file systems are present, use the df command.

The options we're using are:

  • -h (human): Use sensible, best-fit units.
  • --total: Display a line with totals at the bottom of the output.
  • --type=tmpfs: Only report on the tmpfs file systems.

df -h --total --type=tmpfs

df -h --total --type=tmpfs in a terminal window

The first thing that hits you when you look at those values is that they are many times larger than the figure in the Shared column. The sizes shown here are the maximum sizes of these file systems. In reality, they each only occupy as much memory as they need. The figure in the Shared column is the one to believe for memory usage.

What do these file systems hold? Here's a quick breakdown:

  • /run: This holds many temporary files such as PID files, systemd journaling that doesn't have to be preserved across reboots, information to do with Unix-domain sockets, FIFOs, and the management of daemons.
  • /dev/shm: This allows the implementation of POSIX-compliant memory management on Debian and Debian-derived Linux distributions.
  • /run/lock: This holds lock files. These are used as indicators to let the system know a file or other shared resource is in use. They contain the PID of the process using that resource.
  • /sys/fs/cgroup: This is a central element of the scheme that manages control groups. Processes are organized into hierarchical groups according to the types of resources they use. It allows the use of the resources by the processes to be monitored and limited.
  • /run/user/121:  This is a folder created by pam_systemd to store temp files for a user. In this case, the user has an ID of 121. Note that the "user" might be a regular user, a daemon, or some other process.
  • /run/user/1000: This is a folder created by pam_systemd to store temp files for this user, who has the user ID of 1000. This is the current user, user dave.

The Buffer and Cache Columns

The Buffer and Cache columns only appear if you have used the -w (wide). Without the - w option, the figures from these two columns are combined into the Buff/cache column.

These two memory areas interact and depend on one another. The cache area holds (mainly) data that has been read from the hard drive. It is retained in case you need to access it again. It is faster to do so by pulling that data from the cache than reading it back from the hard drive. The cache can also hold data that has been modified but not yet written back to the hard drive, or values that have been computed and not yet saved to a file.

To keep track of the various file fragments and data stashes, the kernel builds an index to the cache memory area, in the buffers memory area. Buffers are portions of memory that hold disk block and other information structures. These contain data about the data that is held in the cache memory area. So the buffers are metadata for the cache.

When a file read request is made, the kernel reads the data in the buffer data structures looking for the file or file fragment that has been requested. If it is found, the request is serviced from the cache memory area pointed to by the buffer data structures. If it is not present in the cache---and so isn't in the metadata in the buffers memory area---the file is read from the hard drive.

The structures in the buffer memory area are:

  • Buffer heads: Each buffer is described in a block of data called a buffer head. Also, if the data in the block is changed and the associated memory page "dirtied", the descriptor tracks the need to write the data back to the hard drive.
  • Inodes: Inodes hold metadata about files and directories, including where they are on the hard drive (or virtual file system), the file size, and the timestamps of the file.
  • Dentries: A dentry (directory entry) is a structure that holds directory listing information. Think of these as a list of inodes for the files and directories within a directory.

You can see why it makes sense to condense the memory used for the buffer and cache memory areas into a single Buff/cache column. They're like two parts of the same thing. The cache memory area would be useless without the buffer memory area providing an index to its contents.

The Available Column

The available column is the sum of the Free column plus the portions of the Buffers and Cache columns (or the Buff/cache column) that can be relinquished immediately. The Available column is an estimate, not an exact figure. It's an informed estimate and an accurate one, but it shouldn't be taken as accurate to the last byte.

Changing the Display Units

To change units that free displays the figures in, use one of the following options.

  • -b: Displays the values in bytes.
  • -k: Displays the values in kibibytes (which is the default).
  • -m: Displays the values in mibibytes.
  • -g: Displays the values in gibibytes.
  • -h: Displays the values in sensible best-fit, units (human-readable) units.

For example, to use human-readable values, use the -h option:

free -h

free -h in a terminal window

free will use the most appropriate unit for each value. As you can see, some of the values are displayed in MiB, and some of them are in GiB.

Displaying a Total

The --total option cause free to display a total line that sums the values from the Total, Used, and Free columns of the Mem and Swap lines.

free -h --total

free -h ---total in a terminal window

The Count Option

The -c (count) option tells free to run for a certain number of times, with a pause of one second between each one. To have free run twice, use this command:

free -h -c 2

free -h -c 2 in a terminal window

Running free Continually

If you want to see the effect a certain application has on your memory usage, it can be useful to have free running continually. This lets you run free in a terminal window while you launch, use, and then close the application you're investigating.

The -s (seconds) option determines the duration of the pause between each run of free. To have free run continually with a three-second pause between each update, use this command:

free -s 3

free -s 3 in a terminal window

Press Ctrl+C to stop the process and return to the command prompt.

Combining the Count and Seconds options

To have free run with a specified pause between each update but stop after a certain number of reports, combine the -s (seconds) and -c (count) options. To have free run five times with a two-second pause between each update, use this command:

free -s 2 -c 5

free -s 2 -c 5 in a terminal window

After the five updates have appeared, the process self-terminates, and you are returned to the command prompt.

self-termination of the free -s 2 -c 5 command in a terminal window

Separating Low and High Memory

This is of little use nowadays, but if you are running Linux on a 32-bit computer, it might prove useful. It separates the memory usage from low memory and high memory.

On a 32-bit Linux based operating system, the CPU can address a maximum of 4GB of memory. The memory is divided into low memory and high memory. Low memory is directly mapped to the kernel's part of the address space. High memory has no direct kernel mapping. High memory is usually anything above 896 MB.

This means the kernel itself (including its active modules) can only make use of low memory. User processes---anything that is not the kernel itself---can potentially make use of low and high memory.

On a 64-bit computer there will be no values shown for high memory:

free -h -l

free -h -l in a terminal window

Memories are Made of This

A quick recap:

  • Total: The amount of RAM installed in your system.
  • Used: Equal to Total-(Free+Buffers+Cache).
  • Free: The amount of memory completely unused by anything.
  • Shared: Memory taken by the tmpfs file systems.
  • Buffer: The data structures that are maintained to provide an index for everything stored in Cache.
  • Cache: Data read from the hard drive, modified data waiting to be written back to the hard drive, and other computed values.
  • Available: What's really free. An estimate of the memory in Free, Buffer, and Cache that could be used to satisfy a memory request.

Linux Commands

Files

tar · pv · cat · tac · chmod · grep ·  diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr

Processes

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld

RELATED: Best Linux Laptops for Developers and Enthusiasts