How to Use the Command 'free' (with examples)

How to Use the Command 'free' (with examples)

The free command is a fundamental utility in Unix-like operating systems that provides a user with a snapshot of the system’s memory usage. It displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel. This command is immensely helpful for system administrators and users looking to monitor or troubleshoot memory-related performance issues in real time. By gaining insights into memory usage patterns, users can manage resources more effectively and ensure that applications are running efficiently.

Displaying System Memory

Code:

free

Motivation:
Monitoring system memory usage is crucial for ensuring that applications and processes have enough resources to function correctly. Regular checks can help diagnose performance bottlenecks that arise due to insufficient memory or memory leaks. The basic command free provides a clear overview of total, used, and free memory available on the system, including both physical and swap memory.

Explanation:
The command free without any options retrieves the system’s memory data in a standardized format. The output details various memory metrics such as total memory, used memory, free memory, shared memory, buffers/cache, and available memory.

Example Output:

              total        used        free      shared  buff/cache   available
Mem:        16392684     7834980      144104     2313856     8369596     5897304
Swap:       2097148      102748     1994400

In this output, you can see a concise breakdown of memory statistics, where ‘Mem:’ refers to the RAM and ‘Swap:’ represents the swap space.

Displaying Memory in Bytes/KB/MB/GB

Code:

free -b

or

free -k

or

free -m

or

free -g

Motivation:
Different projects and system requirements might necessitate viewing memory statistics in specific units for detailed analysis or reporting purposes. For instance, developers might need exact byte-level memory for low-level applications, while IT professionals might prefer an overview in gigabytes for a streamlined outlook during infrastructure audits.

Explanation:

  • -b, -k, -m, -g: These flags determine the units in which the memory statistics are displayed. -b shows bytes, -k shows kilobytes (default), -m shows megabytes, and -g shows gigabytes. By selecting the appropriate unit, users can focus on the granularity or scale that fits their needs best.

Example Output (assuming free -m is used):

              total        used        free      shared  buff/cache   available
Mem:           16005          7460          140          2261          8446          5755
Swap:             2047          100          1947

This output presents the memory statistics in megabytes, providing a mid-level overview that balances detail with readability.

Displaying Memory in Human-Readable Units

Code:

free -h

Motivation:
For users who prefer a straightforward, human-friendly overview of the system’s memory usage, the -h option is ideal. It automatically adjusts the memory units into a more interpretable format, such as GB or MB, depending on the value size, making it much easier for non-technical users to grasp at a glance.

Explanation:

  • -h: This option enables human-readable output. The output automatically scales the memory figures to the most suitable unit — KB, MB, or GB — depending on the quantity, thus typically simplifying the understanding of memory usage without manual conversions.

Example Output:

              total        used        free      shared  buff/cache   available
Mem:           15.7G        7.4G        136M        2.2G        8.2G        5.6G
Swap:          2.0G        100M        1.9G

Here, the memory values are neatly shown with automatic unit adjustments, improving comprehension.

Refreshing the Output Every 2 Seconds

Code:

free -s 2

Motivation:
In environments where memory usage patterns vary quickly and real-time monitoring is required, continuously refreshing the memory statistics can be crucial. This is particularly useful in dynamic server settings, active development environments, or during performance testing where instantaneous updates can inform on-the-fly decisions.

Explanation:

  • -s 2: This option sets the terminal to refresh and display the memory usage statistics every 2 seconds, thereby providing a continuous live feed of memory changes. The number ‘2’ determines the refresh interval in seconds; users can adjust this based on their monitoring needs.

Example Output:

Every 2.0s: free                                                                                

              total        used        free      shared  buff/cache   available
Mem:           15.7G        7.4G        136M        2.3G        8.2G        5.6G
Swap:          2.0G        100M        1.9G

With this continuous output appearing in real-time every 2 seconds, users can actively observe how running processes and system activity influence memory usage.

Conclusion:

Understanding how the free command functions, including its various use cases and options, empowers system administrators, developers, and technical users to monitor and manage system memory more effectively. Each flag tailors the command’s output to suit different needs, from high precision byte-level tracking to user-friendly aggregate views, and real-time monitoring capabilities. These tools are pivotal in ensuring optimal system performance and reliability.

Tags :

Related Posts

How to Use the Command `brew list` (with Examples)

How to Use the Command `brew list` (with Examples)

brew list is an integral command for users of Homebrew, a popular package manager for macOS and Linux.

Read More
Understanding the 'zdb' Command for ZFS Debugging (with examples)

Understanding the 'zdb' Command for ZFS Debugging (with examples)

The ‘zdb’ command stands for ZFS debugger, a powerful utility that provides insights into the configuration and usage of ZFS storage pools, or zpools.

Read More
How to Use 'handbrakecli' (with Examples)

How to Use 'handbrakecli' (with Examples)

HandBrakeCLI is the command-line interface version of the popular HandBrake video conversion software.

Read More