I wrote this article originally for Turing
Regular memory checks are crucial to maintaining the performance of your Linux system. Knowing your system's memory can also aid in debugging and preventing poor application response times while running memory-intensive programs.
There are several ways to view the RAM on your Linux system using the command line, but for the scope of this article, we will focus on the 'free' command.
In this article, we will discuss what the Linux free command is, how it works, and the benefits of the command.
What is the free
command?
The free command is a Linux command that allows you to check for memory RAM on your system or to check the memory statics of the Linux operating system.
To view your system's memory statics, run the free command in your terminal as shown below:
user@ubuntu:~$ free
total used free shared buff/cache available
Mem: 8029356 794336 6297928 183384 937092 6816804
Swap: 0 0 0
Understanding the output
The output of the free
command provides various metrics related to system memory, including total, used, free, shared, buff/cache, and available. To gain a comprehensive understanding of these fields, it is important to examine each one individually. To begin, we'll focus on the 'Mem' metric, which represents the total amount of physical RAM installed on the system.
Examining the Mem
metrics and its fields
The Mem
metric, as displayed in the output of the free
command, serves as a measurement of the physical RAM (Random Access Memory) installed on a computer system. It offers a comprehensive view of the total amount of installed RAM and its current usage by running programs and processes. By monitoring this metric, it is possible to detect any potential memory-related issues within the system.
The Mem metric also includes several fields that give an overview of the system's memory usage, such as:
Total: This is the total amount of physical RAM on your system.
Used: This shows the amount of memory that has been used up or amount of RAM that is currently being utilized by running programs and processes.
Free: This is the amount of physical memory that is not currently being used by any running processes and is ready to be allocated to new processes.
Shared: This displays the total amount of memory used by the temporary
tmpfs
file system.Tmpfs
is a file system that stores files in the computer's main memory (RAM) making it faster to access compared to traditional storage methods like a hard drive.Buff/cache: This is the memory that the kernel (operating system) uses to store recently used data so that it can be accessed quickly. It is used to speed up the performance of the computer by reducing the amount of time it takes to access data from the hard drive. Think of it like a temporary storage area where the computer stores data that it might need soon, so that it doesn't have to search for it again later.
Available: This shows an estimated value of how many memory resources are still open for use. This value can fluctuate as processes start and stop, and memory is freed up and allocated. So, while it may not actively be used by a process at the moment, it is still available to be allocated to a process if needed.
Free and available memory can be tricky to understand. Think of free memory as empty rooms in your house that are ready to be occupied, representing the amount of physical memory that is not currently being used by any running processes and is ready to be allocated to new processes. You can also think of available memory as the total number of rooms that can be occupied including the empty ones and the ones in use for caching and buffering.
An example of caching would be storing items in a storage room that you frequently use, so that you can easily access them when you need them. Buffering would be like having a guest room ready to be used in case you have unexpected visitors. Both the storage room and guest room are being used, but they are still considered "available" because they can be used for their intended purpose if necessary.
Examining the swap
metrics and its fields
The swap metric shows the amount of swap space that is currently being used and the amount of swap space that is available for use.
Swap, also known as virtual memory, is a mechanism that enables computer systems to use extra memory by creating a file or partition on a storage volume. This serves as a backup option when the system's physical RAM is full and can't accommodate new processes. The operating system transfers data from RAM to the swap space, allowing the system to continue running smoothly.
The swap metric also includes several fields that give an overview of the system's memory usage, such as:
- total: The size of the swap partition or swap file
- used: The amount of swap space in use
- free: The remaining (unused) swap space
Referring back to the previous output gotten when the free command was run, the output fields for swap resulted as zero (0). This entails that there is no swap space made available on the system.
Free command options
The free command can be tailored to show memory usage in any desired format.
- To get the memory information in bytes, add the
-b
option to the command
user@ubuntu:~$ free -b
total used free sharedbuff/cache
available
Mem: 8222060544 700334080 6214823936 188076032 1306902528 7087677440
Swap: 0 0 0
- To get the memory information in kilobytes, add the
-k
option to the command.
When the free command is used alone, as was demonstrated earlier, without any option, the default displayed of the memory is in kilobytes.
user@ubuntu:~$ free -k
- To view the memory information in megabytes, add the
-m
option to the command
user@ubuntu:~$ free -m
total used free shared buff/cache available
Mem: 7841 668 5925 179 1247 6759
Swap: 0 0 0
- To view the memory information in gigabytes, add the
-g
option to the command
user@ubuntu:~$ free -g
total used free shared buff/cache available
Mem: 7 0 5 0 1 6
Swap: 0 0 0
- To view the memory information in human-readeable format, add the
-h
option to the command
user@ubuntu:~$ free -h
total used free shared buff/cache available
Mem: 7.7Gi 675Mi 5.8Gi 179Mi 1.2Gi 6.6Gi
Swap: 0B 0B 0B
Benefits of the free
command in Linux
The free
command is an invaluable tool for managing and monitoring memory usage. In this section, we will delve into the benefits of utilizing this command.
Display current memory usage
Running the free command without any arguments will display the current amount of used and available memory on the system, as well as the amount of memory being used for system buffers and disk cache. This helps you to know processes eating up your systems storage.Monitor memory usage over time
By using the free command in combination with the watch command, you can display the current memory usage at regular intervals. For example, "watch -n 5 free -m" will display the current memory usage every 5 seconds. This can be useful for identifying patterns in memory usage over time.Identify memory leaks
If the "used" column of the free command output is consistently high, it may indicate a memory leak in one of the running programs. By running the command periodically and checking the used memory, you can identify the process that's causing the leak.Check for high buffer/cache usage
If the "buffers" and "cached" columns are consistently high, it may indicate that the system is using a lot of memory for caching. While this is generally not a problem, it can cause slow performance if the system is low on memory.Display memory usage in different units
By using the -m or -g options, you can display the memory usage in megabytes or gigabytes, respectively.Comparison with other command-line tools
The free command can be used in conjunction with other command-line tools like top, htop, and vmstat, to provide a more complete picture of the system's memory usage and performance.
Conclusion
In this article, we discussed the free command, how it works and its benefits.
The free
command in Linux is a useful tool for monitoring the system's memory usage as well as a valuable tool for managing and optimizing the performance of Linux systems. Understanding the output of the free command can help administrators and users identify potential memory bottlenecks and troubleshoot performance issues.
Top comments (0)