How to use the command 'free' (with examples)
- Linux
- December 25, 2023
The ‘free’ command is used to display the amount of free and used memory in the system. It provides information about the memory allocation and usage in various units. This command is useful for monitoring system memory and identifying any potential memory issues.
Use case 1: Display system memory
Code:
free
Motivation: This use case is helpful when you want to quickly check the overall memory usage of your system. By running the ‘free’ command without any arguments, you will get a summary of the total memory, used memory, free memory, shared memory, and buffers/cache.
Explanation: Running the ‘free’ command without any arguments provides a summary of the system memory. It shows the total memory available, the memory used by the system, the free memory, the shared memory, and the memory used for buffers and cache.
Example output:
total used free shared buffers cache
Mem: 8192000 2526000 5666000 53000 270000 1294000
-/+ buffers/cache: 961000 7231000
Swap: 0 0 0
Use case 2: Display memory in Bytes/KB/MB/GB
Code:
free -b
or
free -k
or
free -m
or
free -g
Motivation: In certain scenarios, it may be important to view the memory usage in specific units, such as Bytes, Kilobytes, Megabytes, or Gigabytes. Displaying the memory in different units allows for a better understanding of the memory allocation and usage.
Explanation: The ‘-b’ option shows the memory utilization in Bytes, ‘-k’ shows it in Kilobytes, ‘-m’ in Megabytes, and ‘-g’ in Gigabytes. By specifying the desired unit, the output of the ‘free’ command will be displayed accordingly.
Example output:
total used free shared buffers cache
Mem: 8388608000 2573519360 5815088640 62617600 282066688 1357298176
-/+ buffers/cache: 933153496 7455454504
Swap: 2147483648 0 2147483648
Use case 3: Display memory in human-readable units
Code:
free -h
Motivation: The ‘-h’ option makes the ‘free’ command output more human-readable by automatically choosing suitable units (e.g., Gigabytes, Megabytes, etc.) based on the memory size.
Explanation: By using the ‘-h’ option, the ‘free’ command automatically selects the most appropriate unit (e.g., G, M) based on the memory size. This makes the output easier to read and understand.
Example output:
total used free shared buffers cache
Mem: 7.9G 2.4G 5.5G 161M 271M 1.3G
-/+ buffers/cache: 900M 6.9G
Swap: 0B 0B 0B
Use case 4: Refresh the output every 2 seconds
Code:
free -s 2
Motivation: In some situations, you might need to monitor the memory usage in real-time. The ‘-s’ option allows you to refresh the output at a specified interval, enabling you to observe any changes in memory usage over time.
Explanation: By using the ‘-s’ option, followed by the interval in seconds, the ‘free’ command updates the memory usage output at the specified frequency. This allows you to continuously monitor the memory utilization and detect any fluctuations.
Example output:
total used free shared buffers cache
Mem: 8192000 2526316 5665684 53000 270000 1294032
-/+ buffers/cache: 961284 7230716
Swap: 0 0 0
total used free shared buffers cache
Mem: 8192000 2523804 5668196 53000 270020 1293692
-/+ buffers/cache: 959092 7232908
Swap: 0 0 0
Conclusion:
The ‘free’ command is a powerful tool for monitoring system memory usage. By using different options, you can customize the output to suit your needs, such as displaying memory in specific units or refreshing the output at regular intervals. Understanding memory allocation and utilization is crucial for maintaining optimal system performance.