Operations 32 min read

Master Linux Performance: Deep Dive into top, free, and vmstat Commands

This article provides a comprehensive guide to Linux performance monitoring by explaining the usage, options, and output interpretation of the top, free, and vmstat commands, complete with practical examples, field breakdowns, and tips for effective system analysis.

Efficient Ops
Efficient Ops
Efficient Ops
Master Linux Performance: Deep Dive into top, free, and vmstat Commands

1. top Command

The top command is a common Linux performance analysis tool that displays real‑time resource usage of processes, similar to Windows Task Manager.

It updates continuously and can be controlled with interactive keys. The command format is:

<code>top [options]</code>

Key functions include showing process ID, memory usage, CPU usage, and more.

Important options:

-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> monitor specific PID

-n<count> limit number of updates

Example – display all processes:

<code>top</code>

Sample output (truncated) shows system time, load average, task counts, CPU states, memory statistics, swap usage, and a list of processes with fields such as PID, USER, PR, NI, VIRT, RES, SHR, S, %CPU, %MEM, TIME+, and COMMAND.

Interpretation of the first five lines:

Current time, uptime, number of users, and load averages for 1, 5, and 15 minutes.

Task summary: total, running, sleeping, stopped, zombie.

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.

Swap: total, used, free, cached.

Additional tips:

Press 1 to view per‑CPU usage.

Press b to toggle highlighting of the current running process.

Press x to toggle column highlighting for sorting.

Use Shift + > or Shift + < to change the sort column (e.g., sort by %MEM).

2. free Command

The free command displays the amount of free and used physical memory, swap memory, and buffers used by the kernel.

Command format:

<code>free [options]</code>

Key functions:

Shows total, used, free memory.

Shows shared memory, buffers, cached memory.

Provides a line with "-/+ buffers/cache" to indicate memory actually available to applications.

Important options:

-b display in bytes

-k display in kilobytes (default)

-m display in megabytes

-g display in gigabytes

-o omit the buffer column

-s<seconds> continuously monitor

-t show total memory line

-V show version

Example output:

<code>[root@SF1150 service]# free
             total       used       free     shared    buffers     cached
Mem:      32940112   30841684    2098428          0    4545340   11363424
-/+ buffers/cache:   14932920   18007192
Swap:     32764556    1944984   30819572</code>

Interpretation:

total : total physical memory.

used : memory in use (including kernel buffers and cache).

free : memory not yet allocated by the kernel.

buffers and cached : memory used for disk buffering and page cache.

The "-/+ buffers/cache" line shows memory actually usable by applications.

From an application perspective, usable memory = free + buffers + cached.

3. vmstat Command

vmstat (Virtual Memory Statistics) reports statistics about virtual memory, processes, CPU activity, and I/O.

Command format examples:

<code>vmstat [-a] [-n] [-S unit] [delay [count]]
vmstat -s
vmstat -m
vmstat -d
vmstat -p /dev/sda1</code>

Key functions:

Shows processes (r, b), memory (swpd, free, buff, cache), swap activity (si, so), I/O (bi, bo), system interrupts (in, cs), and CPU usage (%us, %sy, %id, %wa).

Important options:

-a display active and inactive memory.

-s display memory‑related statistics.

-m display slabinfo.

-d display disk statistics.

-p <device> display statistics for a specific block device.

-t show totals.

-V show version.

Example – basic usage:

<code>vmstat</code>

Sample output fields:

r : runnable processes.

b : processes blocked for I/O.

swpd : virtual memory used.

free : idle memory.

buff : memory used as buffers.

cache : memory used as page cache.

si / so : swap in/out per second.

us / sy / id / wa : CPU time spent in user, system, idle, and I/O wait.

Additional examples illustrate how to monitor active/inactive memory (

vmstat -a 2 5

), count forks (

vmstat -f

), view detailed memory statistics (

vmstat -s

), and examine slab caches (

vmstat -m

).

Understanding these metrics helps identify CPU bottlenecks, memory pressure, I/O contention, and overall system health, which is essential for effective Linux operations and performance tuning.

performance monitoringLinuxtop commandfree commandvmstat
Efficient Ops
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.