In-Depth Understanding of <span>free</span> Command: Quickly Check Memory Usage in Linux Systems
In the daily management and operation of Linux systems, understanding the system’s memory usage is a very important task. Memory is one of the key resources for system performance, and improper memory usage can lead to system slowdowns, program crashes, or even service unavailability. So, is there a simple and direct command to quickly check the current memory usage of the system?The answer is yes, and that command is:
<span>free</span>
1. What is the function of the <span>free</span> command?
<span>free</span> command is a basic and practical tool used in Linux and Unix-like systems to view system memory usage. It can display:
- The system’s physical memory (RAM) usage
- Swap space usage
- Buffers and cache memory usage
- And other key information such as available memory.
In simple terms, the <span>free</span> command is used to tell you: “How much memory is currently used by your system? How much is left? Is swap space being used? How much memory is occupied by cache?”
2. How to use the <span>free</span> command?
Basic usage:
free
After running, you will see output similar to the following (default unit is KB):
total used free shared buff/cache available
Mem: 8076700 3124300 1023400 123400 3928000 4512300
Swap: 2048252 0 2048252
For better readability, we usually add the
<span>-h</span>option to display in a “human-readable” format (like MB, GB).
Recommended usage: Display in readable units (MB / GB)
free -h
Example output:
total used free shared buff/cache available
Mem: 7.7G 3.0G 1.0G 120M 3.7G 4.3G
Swap: 2.0G 0B 2.0G
β
<span>-h</span>option means “human-readable”, which will automatically display in appropriate units like KB, MB, GB, etc., making it very intuitive.
3. Detailed explanation of the <span>free</span> command output
Let’s focus on interpreting the meaning of each column in the output of <span>free -h</span>:
1. Mem (Physical Memory)
| Column Name | Description |
|---|---|
| total | Total physical memory size of the system, i.e., the installed RAM capacity |
| used | Memory that has been used (note: includes cache and buffers) |
| free | Memory that is completely unused (note: this value is usually small and does not indicate memory pressure) |
| shared | Memory shared by multiple processes (less focus) |
| buff/cache | Memory used by the kernel for buffers and cache, which can be quickly reclaimed |
| available | System available memory, including unused memory + reclaimable cache, is the most important indicator for assessing whether memory is sufficient β |
π Key focus:
- available: This is the true indicator of “how much memory is still usable”, which is more valuable than free.
- buff/cache: Linux utilizes free memory as disk cache to improve performance, and this memory will be automatically released when applications need it.
2. Swap (Swap Space)
| Column Name | Description |
|---|---|
| total | Total size of the swap partition |
| used | Used swap space |
| free | Unused swap space |
π Note:
- If Swap used is close to or large, it indicates that physical memory may be insufficient, and the system has started using swap space on the hard disk, which will lead to performance degradation (because hard disk is much slower than memory).
- Ideally, Swap used should be small or 0, indicating sufficient physical memory.
4. Other common options for the <span>free</span> command
| Command | Description |
|---|---|
<span>free</span> |
Displays memory usage in KB by default |
<span>free -h</span> |
Recommended! Displays in human-readable units (MB/GB), most commonly used |
<span>free -b</span> |
Displays in bytes |
<span>free -k</span> |
Displays in KB (default) |
<span>free -m</span> |
Displays in MB |
<span>free -g</span> |
Displays in GB (note: will round down) |
<span>free -t</span> |
Displays a summary line (Total line) of memory totals |
<span>free -s 2 -c 5</span> |
Refreshes every 2 seconds, displaying a total of 5 times (for dynamic observation) |
Example: Dynamically view memory changes (refresh every 2 seconds, total 5 times)
free -h -s 2 -c 5
This will refresh the memory usage every 2 seconds and output a total of 5 times, which is very suitable for observing the dynamic changes in memory usage, such as checking memory growth while running a program.
5. Common Questions and Misconceptions
β Why is the <span>free</span> value small, but my system seems to be running fine?
This is because Linux makes full use of free physical memory as disk cache and buffers, thereby speeding up file read and write operations. This memory will be automatically released when applications need it, so there is no need to worry.You should pay more attention to the <span>available</span> column, which indicates the actual available memory of the system, including unused memory and quickly reclaimable cache.
β When should I pay attention to Swap?
- If Swap used is close to or large, it indicates that physical memory may be insufficient, and the system has started using swap space on the hard disk, which will significantly slow down system performance.
- Ideally, Swap usage should be close to 0, especially for servers or workstations with large memory.
β Is it necessary to manually clear high <span>buff/cache</span> usage?
Generally, it is not necessary. This memory is actively utilized by Linux to enhance performance, and when applications need more memory, the system will automatically release the cache. If you really need to manually clear it (for example, for testing), you can use the following command:
sync; echo 3 > /proc/sys/vm/drop_caches
β οΈ Note:Use with caution in production environments! Clearing the cache may lead to a temporary drop in system performance, as the cache needs to be rebuilt after being cleared.
6. Summary in One Sentence
<span>free</span>command is a basic tool in Linux systems for quickly viewing memory and swap space usage. By using<span>free -h</span>, you can intuitively understand the usage status of physical memory, cache, and swap partitions, where the<span>available</span>column is the most important indicator for determining whether memory is sufficient, making it an indispensable tool in system monitoring and performance tuning.
7. Suggestions for Further Learning
If you want to analyze system performance more deeply, you can also use the following commands in conjunction:
<span>top</span>/<span>htop</span>: View process-level resource usage, including memory and CPU<span>vmstat</span>: View comprehensive information on virtual memory, processes, CPU activity, etc.<span>vmstat 1</span>: Dynamically view memory, swap, IO, and CPU status<span>glances</span>/<span>nmon</span>: More comprehensive system monitoring tools
Mastering the <span>free</span> command allows you to quickly understand key information such as “Is there enough memory in the system? Is swap space being used? How much memory is occupied by cache?” It is a fundamental command that every Linux user and administrator should be proficient in.Now open your terminal and type:
free -h
to check your system’s memory usage!π‘π₯οΈπ