linux free command – Introduction
linux free command is an effective tool that provides information on memory usage in a comprehensible manner.
According to the man page for this command, free shows the total amount of free and used memory on the system, including the physical memory, swap space, and the kernel’s used buffers and caches. /proc/meminfo is parsed to obtain the data.
Free has undergone adjustments throughout time, much like any other command, to make its output accurate and meaningful for decision-making. The output in Red Hat Enterprise Linux (RHEL) 6 differs slightly from that in RHEL 7 and RHEL 8. There are important factors that remain the same in all these iterations, though.
How to use free command in linux
In Linux, the ‘free’ command is used to display information about the system’s memory usage. It provides details about the total amount of free, used, and available physical and swap memory. Here’s how you can use the “free” command in Linux:
Open a terminal: On your Linux system, start a terminal emulator. Typically, you may do this by typing “Terminal” into the programmes menu or by pressing the Ctrl+Alt+T keyboard shortcut.
Run the “free” command: Once you have the terminal open, simply type the following command and press Enter:
free
Interpret the output: The “free” command will display information about memory usage in kilobytes (KB). The output will consist of several columns:
- total: Your system’s overall physical memory (RAM) capacity.
- used: How much memory the system is using.
- free: Unallocated RAM that is not being used by the system.
- shared: Memory shared between different processes.
- buffers/cached: Memory used by the kernel for buffers and cache.
- available: Amount of memory available for starting new applications without swapping.
The values for each category will be displayed for both physical memory (under “Mem”) and swap memory (under “Swap”).
Different Options in Linux ‘free’ Command
Here are 15 options for the Linux free command, explained below:
- ‘free’: Default Memory Usage
This option displays memory usage in kilobytes, including total, used, and free memory.
- ‘free -h’ or ‘free –human’: Human-Readable Memory Usage
This option provides memory usage in a human-readable format, using units like bytes, kilobytes, megabytes, and gigabytes.
- ‘free -b’ or ‘free –bytes’: Memory Usage in Bytes
This option displays memory usage in bytes.
- ‘free -k’ or ‘free –kilo’: Memory Usage in Kilobytes
This option displays memory usage in kilobytes.
- ‘free -m’ or ‘free –mega’: Memory Usage in Megabytes
This option displays memory usage in megabytes.
- ‘free -g’ or ‘free –giga’: Memory Usage in Gigabytes
This option displays memory usage in gigabytes.
- ‘free -s <seconds>’ or ‘free –seconds <seconds>’: Continuous Refresh
By specifying the number of seconds, this option refreshes the output at the specified interval. For example, free -s 2 refreshes the output every 2 seconds.
- ‘free -t’ or ‘free –total’: Total Memory Usage
This option adds an extra line at the bottom of the output, showing the total memory usage, including buffers and cache.
- ‘free -o’ or ‘free –old’: Old Format
This option displays memory usage in the old format, without the header row.
- ‘free -l’ or ‘free –lohi’: Low and High Memory Statistics
This option shows the low and high memory statistics. Low memory is the memory used for kernel data structures, and high memory is the user-space memory.
- ‘free -s’ or ‘free –summary’: Memory Summary
This option provides a summary of memory statistics, including total, used, and free memory.
- ‘free -c <count>’ or ‘free –count <count>’: Display Count
With this option, you can specify the number of times the command should refresh and display the memory usage.
- ‘free -w’ or ‘free –wide’: Wide Output
This option displays memory usage with wider output, providing more space for displaying values.
- ‘free -k -t’ or ‘free –kilo –total’: Total Memory Usage in Kilobytes
This combination of options displays memory usage in kilobytes and includes the total memory usage.
- ‘free -m -t’ or ‘free –mega –total’: Total Memory Usage in Megabytes
This combination of options displays memory usage in megabytes and includes the total memory usage.
These options offer flexibility when using the free command, allowing you to customize the output based on your requirements.
The benefits of the ‘free’ command
Here are some benefits of using the free command in Linux, explained below:
Memory Usage Overview: The ‘free’ command provides a quick overview of memory usage on a Linux system. It displays information such as total memory, used memory, free memory, shared memory, buffers, and cached memory. This helps in understanding the overall memory utilization.
Swap Space Usage: In addition to memory usage, the ‘free’ command also shows information about swap space. When the system’s physical memory is exhausted, swap space, a piece of the hard drive, is used as virtual memory. By displaying swap information, the command allows you to monitor and manage swap space effectively.
Customization Options: The ‘free’ command offers various options to customize the output based on your requirements. You can choose to display memory values in different units (bytes, kilobytes, megabytes, or gigabytes) using options like ‘-b’, ‘-k’, ‘-m’, or ‘-g’. This flexibility allows you to view memory information in a format that suits your needs.
Human-Readable Format: With the ‘-h’ or ‘—human’ option, the ‘free’ command can display memory information in a more readable format. It uses units like “G” for gigabytes and “M” for megabytes, making it easier to interpret and understand memory usage immediately.
Historical and Real-Time Monitoring: The ‘free’ command provides options like ‘-s’ and ‘-c’ that allow you to monitor memory usage over time. You can specify a delay between updates using ‘-s’ to get real-time memory usage updates. With ‘-c’, you can set a count to display memory information multiple times before exiting. These options are useful for monitoring memory usage trends and analyzing system behavior.
Low and High Memory Statistics: The ‘-l’ or ‘—lohi’ option provides detailed low and high memory statistics. It shows the amount of low and high memory used, which can help diagnose and troubleshoot memory-related issues on Linux systems.
Active and Inactive Memory: The ‘-a’ or ‘—active’ option displays active and inactive memory. Active memory represents memory actively used by processes, while inactive memory contains recently accessed data that is still in memory but not currently in use. This distinction helps in understanding the memory utilization patterns and optimizing memory allocation.
Troubleshooting Memory Issues: The ‘free’ command helps in troubleshooting memory-related issues on Linux systems. By analyzing the memory usage metrics provided by the command, you can identify if the system is running low on memory, if swap space is being excessively used, or if there are any abnormal memory patterns. This information is crucial for identifying memory leaks, inefficient resource allocation, or processes consuming excessive memory.
Resource Optimization: The ‘free’ command assists in optimizing resource allocation on Linux systems. By monitoring memory usage, you can identify if the system is underutilizing available memory or if it’s experiencing memory bottlenecks. This information allows you to fine-tune memory allocation, adjust caching settings, or optimize memory-intensive applications for better overall system performance.
Capacity Planning: The ‘free’ command plays a role in capacity planning. By regularly monitoring memory usage trends and understanding memory requirements, you can estimate future resource needs and make informed decisions regarding hardware upgrades or scaling strategies. Capacity planning helps ensure the system has enough memory to handle anticipated workloads efficiently.
Integration with Scripts and Automation: The ‘free’ command can be easily integrated into scripts or automation workflows to gather memory usage statistics programmatically. By incorporating the free command into scripts, you can collect memory data at regular intervals, process it, and perform automated actions based on specific memory thresholds or conditions. This enables proactive memory management and efficient resource allocation in dynamic environments.
Compatibility and Availability: The ‘free’ command is available on most Linux distributions, making it widely compatible and accessible across different systems. Whether you are using Ubuntu, CentOS, Debian, or other Linux distributions, you can rely on the ‘free’ command to provide consistent and reliable memory usage information.
Overall, the ‘free’ command is a versatile and essential tool for monitoring, analyzing, and managing memory usage on Linux systems. Its comprehensive output, customization options, and integration capabilities make it a valuable resource for system administrators, developers, and anyone involved in optimizing system performance and resource utilization.
Final Words
This article covered the free command’s functionality and advantages. The “free” command in Linux is a helpful tool for maintaining and enhancing the efficiency of Linux systems as well as for keeping track of the system’s memory usage. Administrators and users can identify potential memory bottlenecks and solve performance problems by understanding the free command’s output.
You can learn about linux more deeply by clicking the link below
https://linuxiron.com/what-is-linux-a-whole-introduction/
Learn about the other linux commands by clicking the links below
https://linuxiron.com/echo-command-in-linux/
https://linuxiron.com/how-to-use-nice-renice-commands-in-linux/
https://linuxiron.com/how-to-use-kill-commands-in-linux/
https://linuxiron.com/a-beginners-guide-to-htop-for-process-management/
https://linuxiron.com/15-useful-yum-commands-in-linux/
https://linuxiron.com/how-to-use-the-top-command-in-linux/
https://linuxiron.com/17-ps-command-to-monitor-linux-process-with-examples-linuxiron/
https://linuxiron.com/12-cat-commands-in-linux-with-examples/