Using free and vmstat

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,526
Reaction score
3,292
Credits
31,543

Monitoring Memory Usage in Linux with free and vmstat​

Monitoring memory usage is crucial for maintaining the performance and stability of your Linux system. Two powerful commands that can help you with this task are free and vmstat. In this article, we'll explore how to use these commands to get detailed information about your system's memory usage.

Using the free Command​

The free command provides a quick overview 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 and caches used by the kernel.

Basic Usage​

To display memory usage, simply run:

Code:
 free

This will output something like:

Code:
 total used free shared buff/cache available Mem:
16384256 1234567 2345678 345678 4567890 5678901 Swap: 8388608 123456 8265152

Detailed Output​

For more detailed information, you can use the -h option to display the output in human-readable format:

Code:
 free -h

This will show the memory usage in units like KB, MB, or GB, making it easier to read. For example:

Code:
 total used free shared buff/cache available Mem:
15G 1.2G 2.3G 345M 4.4G 5.4G Swap: 8.0G 123M 7.9G

Using the vmstat Command​

The vmstat (virtual memory statistics) command provides a more detailed view of system performance, including memory, CPU, and I/O statistics. It's particularly useful for identifying performance bottlenecks.

Basic Usage​

To display a summary of system performance, run:

Code:
 vmstat

This will output something like:

Code:
 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 
1 0 123456 2345678 345678 4567890 0 0 1 2 123 456 7 8 85 0 0

Real-Time Monitoring​

To monitor memory usage in real-time, you can specify a delay between updates. For example, to update every 2 seconds, run:

Code:
 vmstat 2

This will continuously display updated statistics every 2 seconds until you stop it with Ctrl+C.

Accurate Real-Time Data​

When you run the vmstat command, the first line of output represents the average values since the last reboot, which might not be very useful for real-time monitoring. To get a more accurate and current view of your system's performance, it's recommended to look at subsequent lines of output. For example, running vmstat 2 4 will give you four lines of output with a 2-second interval between each line:

Code:
 vmstat 2 4

This will output something like:

Code:
 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 123456 2345678 345678 4567890 0 0 1 2 123 456 7 8 85 0 0 0
0 123456 2345678 345678 4567890 0 0 1 2 123 456 7 8 85 0 0 0
0 123456 2345678 345678 4567890 0 0 1 2 123 456 7 8 85 0 0 0
0 123456 2345678 345678 4567890 0 0 1 2 123 456 7 8 85 0 0

By examining the subsequent lines, you can get a more accurate picture of your system's current performance.

Combining free and vmstat​

While free provides a quick snapshot of memory usage, vmstat offers a more comprehensive view of system performance. Using both commands together can give you a better understanding of your system's memory usage and help you identify potential issues.

Example Script​

Here's a simple script that uses both commands to monitor memory usage:

Code:
 #!/bin/bash echo "Memory usage overview:" free -h echo "" echo "Detailed system performance:" vmstat 2 5

This script will display a snapshot of memory usage and then provide detailed performance statistics for 10 seconds (5 updates at 2-second intervals).

Conclusion​

By using the free and vmstat commands, you can effectively monitor and manage memory usage on your Linux system. These tools provide valuable insights that can help you maintain optimal system performance and quickly identify and resolve issues.
 


Members online


Top