Linux free
Command
In the realm of Linux and Unix-like operating systems, monitoring system
memory
is a critical aspect of system administration. The free
command
is
a powerful tool that provides essential insights into system memory usage,
allowing users and administrators to track memory availability, usage, and
system performance. In this blog post, we will explore the free
command in depth, covering its syntax, options, and various practical
applications.
Basic Syntax
The basic syntax of the free
command is simple:
bashfree options
options
: Additional flags that modify the behavior of thefree
command.
Displaying Memory Usage
To display memory usage, you can use the free
command without
any
options.
bashfree
This will display information about total memory, used memory, free memory, shared memory, buffers, and cached memory.
Human-Readable Output
The -h
or --human
option makes the output more
human-readable by displaying sizes in a human-readable format (e.g., KB, MB,
GB).
bashfree -h
Displaying Memory Statistics at Regular Intervals
The -s
or --seconds
option allows you to
continuously
monitor memory statistics at regular intervals.
bashfree -s 10
This will display memory statistics every 10 seconds.
Displaying Total and Available Memory Only
The -t
or --total
option displays the total memory
and
available memory only.
bashfree -t
Practical Applications
-
Viewing Memory Usage:
bashfree
-
Displaying Human-Readable Output:
bashfree -h
-
Continuously Monitoring Memory Usage:
bashfree -s 10
Advanced Usage
Displaying Memory Usage in MB
You can use awk
to display memory usage in megabytes (MB).
bashfree -m | awk 'NR==2{print "Total: " $2 "MB", "Used: " $3 "MB", "Free: " $4 "MB"}'
Displaying Memory Usage in Percentage
You can calculate and display memory usage in percentage using a simple command.
bashfree | grep Mem | awk '{printf("Used Memory: %.2f%"), $3/$2*100}'
Monitoring Memory Usage in a Continuous Loop
You can use a while
loop to continuously monitor memory usage.
bashwhile true; do free -h; sleep 10; done
This will display memory usage every 10 seconds in a loop.