Master Linux Performance Monitoring: Top, Free, and Vmstat Commands Explained
This comprehensive guide walks you through Linux performance monitoring tools—top, free, and vmstat—detailing their syntax, key options, real‑world output examples, and how to interpret CPU, memory, swap, and process statistics for effective system administration.
Top Command
The top command is a real‑time Linux performance analysis tool that displays each process's resource usage, similar to Windows Task Manager.
Usage:
top [options]Key options include:
-b batch mode
-c show full command line
-I ignore idle processes
-s secure mode
-S cumulative mode
-i<interval> set refresh interval
-u<user> filter by user
-p<pid> filter by PID
-n<count> number of iterations
Example – display all processes:
<code>top</code>Sample output (truncated) shows system time, uptime, load average, task counts, CPU usage percentages, memory statistics, and a list of processes with columns such as PID, USER, PR, NI, VIRT, RES, SHR, S, %CPU, %MEM, TIME+, COMMAND.
Interpretation of the first five lines:
Current time, uptime, number of users, and load average (1‑, 5‑, 15‑minute averages).
Task summary: total, running, sleeping, stopped, zombie processes.
CPU states: user (%us), system (%sy), nice (%ni), idle (%id), I/O wait (%wa), hardware IRQ (%hi), software IRQ (%si), steal (%st).
Memory: total, used, free, buffers, cached.
Swap: total, used, free, cached.
Additional tips:
Press 1 to toggle per‑CPU view.
Press b to highlight the currently running process.
Press x to toggle column highlighting; default sort column is %CPU .
Use Shift+> or Shift+< to change the sort column (e.g., to %MEM).
Free Command
The free command shows the amount of free and used physical memory, swap space, and buffers/cache.
Usage:
free [options]Important options:
-b display in bytes
-k display in kilobytes (default)
-m display in megabytes
-g display in gigabytes
-o omit the buffer/cache line
-s <seconds> continuously monitor
-t show total memory line
-V show version
Example – show memory in default units:
<code>free</code>Sample output:
<code> total used free shared buffers cached
Mem: 32940112 30841684 2098428 0 4545340 11363424
-/+ buffers/cache: 14932920 18007192
Swap: 32764556 1944984 30819572</code>Key fields:
total – total physical memory.
used – memory in use (including kernel buffers and cache).
free – memory not yet allocated.
shared – memory used by multiple processes.
buffers – memory used for block device buffers.
cached – page cache.
From an application perspective, usable memory = free + buffers + cached.
Continuous monitoring example:
<code>free -s 10</code>Vmstat Command
vmstat (Virtual Memory Statistics) reports information about processes, memory, paging, block I/O, traps, and CPU activity.
Usage examples:
vmstat– one‑time snapshot.
vmstat 5 5– sample every 5 seconds, 5 times.
vmstat -a– show active/inactive memory.
vmstat -s– display memory statistics in human‑readable form.
vmstat -d– show disk I/O statistics.
vmstat -m– show slab allocator info.
vmstat -f– show number of forks since boot.
Key fields in the default output:
procs : r (running queue), b (blocked for I/O).
memory : swpd, free, buff, cache.
swap : si (swap in), so (swap out).
io : bi (blocks received), bo (blocks sent).
system : in (interrupts), cs (context switches).
cpu : us, sy, id, wa, st.
Interpretation tips:
High r (>4) and low id (<40%) indicate CPU overload.
Non‑zero si / so suggest memory pressure causing swapping.
Frequent disk activity with high b (>3) points to I/O bottlenecks.
Example – display virtual memory usage:
<code>vmstat</code>Example – show active/inactive memory:
<code>vmstat -a 2 5</code>Example – count forks since boot:
<code>vmstat -f</code>Example – detailed memory statistics:
<code>vmstat -s</code>Example – disk I/O statistics:
<code>vmstat -d</code>Example – slab allocator details:
<code>vmstat -m</code>Understanding these commands helps administrators diagnose performance issues, plan capacity, and keep Linux systems running smoothly.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.