10 ‘free’ Commands to Check Memory Usage in Linux


If you are a Linux user, you may have heard of term "memory usage". Memory usage refers to amount of memory being used by your computer at any given time. It is an important metric to keep track of, as excessive memory usage can cause your system to slow down or even crash. Fortunately, Linux provides a built-in tool called "free" that allows you to check your system's memory usage. In this article, we will go over how to use "free" command to check memory usage in Linux.

What is "free" Command?

The "free" command is a built-in tool in Linux that displays information about system's memory usage. It provides detailed information about amount of memory being used, amount of free memory available, and amount of memory being used by system's kernel.

How to use "free" Command?

Using "free" command is simple. Just open up a terminal and type in "free" followed by any optional flags. Here's an example −

$ free

This will display memory usage statistics for your system.

Understanding Output of "free" Command

The output of "free" command can be a bit overwhelming, especially if you're new to Linux. However, once you understand different fields, it becomes easier to read and interpret. Here's an example output −

              total        used        free      shared  buff/cache   available
Mem:        8093816     1439556     5458576      195084     1194684     6159384
Swap:       2097148           0     2097148

Here's what each field means −

  • total − total amount of physical memory available to your system.

  • used − amount of memory that is currently being used by your system.

  • free − amount of memory that is currently available for use.

  • shared − amount of memory that is shared between different processes.

  • buff/cache − amount of memory used for disk caching.

  • available − amount of memory that is available for new processes to use.

It's important to note that values displayed in "used" field do not include memory used for disk caching. Instead, this is displayed in "buff/cache" field. "available" field shows amount of memory that is actually available for new processes to use, taking into account memory used for disk caching.

Using "-h" Flag

By default, "free" command displays memory usage statistics in bytes. This can be difficult to read, especially if you're dealing with large amounts of memory. Fortunately, you can use "-h" flag to display output in a more human-readable format. Here's an example −

$ free -h

This will display output in a format that's easier to read, using units such as "MB" and "GB" instead of bytes.

Using "-s" Flag

If you want to continuously monitor your system's memory usage, you can use "-s" flag to specify a delay between each update. For example, following command will display memory usage statistics every 5 seconds −

$ free -s 5

Using "-t" Flag

By default, "free" command displays memory usage statistics for both physical memory and swap space. If you only want to see statistics for physical memory, you can use "-t" flag to exclude swap space statistics. For example −

$ free -t

This will display memory usage statistics for physical memory only.

Here are a few more examples of how to use "free" command with different flags −

Displaying Memory Usage in Megabytes

$ free -m

This will display memory usage statistics in megabytes, which can be easier to read than default format in bytes.

Displaying Memory Usage in Gigabytes

$ free -g

This will display memory usage statistics in gigabytes, which is useful for systems with a large amount of memory.

Displaying Memory Usage in a Continuous Loop with a Delay of 2 Seconds

$ free -s 2 -c

This will display memory usage statistics in a continuous loop, with a delay of 2 seconds between each update. "-c" flag specifies that output should be cleared before each update.

Displaying Memory Usage Statistics for a Specific Process

$ ps aux | grep firefox
$ pmap <PID>

This will display memory usage statistics for a specific process, such as Firefox web browser. First, use "ps" command to find process ID (PID) of process you want to check. Then, use "pmap" command with PID to display detailed memory usage information for that process.

Displaying Memory Usage Statistics for a Specific User

$ sudo su <USERNAME>
$ free -u <USERNAME>

This will display memory usage statistics for a specific user, such as root user or another user on system. First, switch to user account using "sudo su" command. Then, use "free" command with "-u" flag to display memory usage statistics for that user.

Displaying Memory Usage Statistics in a Specific Format

$ free -o

This will display memory usage statistics in a specific format, which can be useful for scripting or parsing output. "-o" flag specifies that output should be in a "long" format.

Displaying Memory Usage Statistics in a Specific Unit

$ free -k

This will display memory usage statistics in kilobytes, which can be useful for systems with limited memory resources.

Displaying Memory Usage Statistics for a Specific Interval

$ free -t 10

This will display memory usage statistics every 10 seconds, which can be useful for monitoring system's memory usage over time. "-t" flag specifies interval in seconds.

Displaying Memory Usage Statistics for a Specific Process Group

$ free -g 1

This will display memory usage statistics for a specific process group, such as a group of processes running in a container or virtual machine. "-g" flag specifies process group ID.

Displaying Memory Usage Statistics for a Specific Memory Type

$ free -b -t | grep "^Swap"

This will display memory usage statistics for a specific memory type, such as swap space. "-b" flag specifies that output should be in bytes, and "grep" command filters output to only show swap space statistics.

Displaying Memory Usage Statistics Sorted by a Specific Field

$ free -m | sort -nk 2

This will display memory usage statistics sorted by "used" column in ascending order, which can be useful for identifying processes or services that are consuming most memory. "sort" command is used to sort output based on a specific column, with "-n" option specifying numerical sorting and "-k" option specifying column to sort by.

Displaying Memory Usage Statistics with a Timestamp

$ while true; do date +"%F %T"; free -h; sleep 5; done

This will display memory usage statistics with a timestamp, which can be useful for tracking changes in memory usage over time. "while" loop is used to continuously run "date" and "free" commands with a delay of 5 seconds between each update.

Displaying Memory Usage Statistics for a Specific Virtual Memory Area

$ free -t | grep "VmallocUsed"

This will display memory usage statistics for a specific virtual memory area, such as "vmalloc" area used by kernel. "grep" command filters output to only show "VmallocUsed" line, which displays amount of memory used by virtual memory area.

Displaying Memory Usage Statistics for a Specific Process ID

$ ps aux | grep firefox
$ cat /proc/<PID>/status | grep VmRSS

This will display memory usage statistics for a specific process ID, such as Firefox web browser. First, use "ps" command to find process ID (PID) of process you want to check. Then, use "cat" command with "/proc/PID/status" file to display detailed memory usage information for that process, such as resident set size (RSS) or size of its code segment.

Conclusion

The "free" command is a powerful tool that allows you to check your system's memory usage in Linux. By understanding different fields in output and using various flags, you can get a detailed picture of how your system is using its memory resources. Monitoring memory usage is an important task for any Linux user, especially if you're running memory-intensive applications or servers. With "free" command, you can quickly and easily check your system's memory usage and take action if necessary.

In addition to "free" command, there are other tools available in Linux for monitoring memory usage. For example, "top" command provides real-time information about system's memory usage, as well as CPU usage and other system statistics. There are also third-party tools available, such as "htop", which provides a more advanced interface for monitoring system resources.

Updated on: 02-May-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements