New Course: Agentic AI with Python
LinuxLinux Packages, Networking & System Administration

System Resource Monitoring in Linux

When a Linux server becomes slow or an application stops responding, one of the first steps is to check system resource usage. High CPU utilization, insufficient memory, or a disk running out of space can affect application performance.

Linux provides several commands to monitor these resources and identify potential bottlenecks. In this lecture, we'll explore five useful commands for checking memory, storage, running processes, and overall system performance.

free - Monitor Memory Usage

The free command displays information about system memory, including RAM and swap space.

free

By default, memory values are displayed in bytes. Use -h to display them in a human-readable format.

free -h

Understanding the Output

               total   used   free  shared  buff/cache  available
Mem:            976M   450M   120M      1M        406M       380M
Swap:             0B     0B     0B
ColumnMeaning
totalTotal memory available to the system
usedMemory currently in use, including memory used for caching
freeMemory that is currently unused
sharedMemory primarily used by shared memory mechanisms
buff/cacheMemory used for buffers and caches
availableEstimated memory available for new applications without significant swapping

Important: Don't judge memory pressure by the free column alone. Linux uses otherwise idle RAM for caching to improve performance. The available column is generally a better indicator of how much memory can be used by applications.


df - Monitor Disk Space

The df command reports the available and used space on mounted filesystems.

df -h

The -h option displays sizes in readable units such as MB and GB.

Example Output

Filesystem      Size  Used  Avail  Use%  Mounted on
/dev/root         8G  3.5G   4.5G   44%  /
ColumnMeaning
FilesystemDevice or filesystem being reported
SizeTotal filesystem capacity
UsedSpace currently used
AvailSpace available for use
Use%Percentage of space used
Mounted onDirectory where the filesystem is accessible

To check the filesystem containing a specific directory, run:

df -h /var/log

This is useful when an application fails to write logs or create files because a filesystem is running out of space.


du - Identify Large Files and Directories

While df shows filesystem usage, du estimates how much disk space individual files and directories consume.

du -sh *

Here:

  • -s displays a summary for each specified item.
  • -h displays sizes in a human-readable format.
  • * matches non-hidden files and directories in the current directory.

Example Output

4.0K    ai.txt
12K     devops
4.0K    error.log

In this example, the devops directory uses more space than the other listed items.

To check the size of a specific directory:

du -sh /var/log

To list the sizes of immediate subdirectories in a directory:

du -h --max-depth=1 /var/log

Practical tip: If df -h shows that a disk is nearly full, use du to investigate which directories are consuming the most space. Remember that du -sh * does not include hidden entries matched by the shell's * pattern.


top - Monitor Running Processes in Real Time

The top command provides a continuously updating view of running processes and system resource usage.

top

It displays information such as:

  • CPU utilization
  • Memory and swap usage
  • Running processes
  • CPU and memory consumption by individual processes
  • Process IDs (PIDs)

Useful Keys in top

KeyAction
qExit top
PSort processes by CPU usage
MSort processes by memory usage
1Show CPU usage for individual logical CPUs

Key behavior can vary slightly across versions and configurations.

When to use it: If an application is slow, use top to identify processes consuming unusually high CPU or memory.

Unlike commands that produce a single report, top is an interactive monitor. For automated or periodic reporting, commands such as vmstat may be more convenient.


vmstat - Monitor System Performance

The vmstat command reports statistics related to processes, memory, paging, block I/O, and CPU activity.

vmstat

To monitor performance at two-second intervals:

vmstat 2

Press Ctrl + C to stop continuous monitoring.

Note: The first report commonly represents averages since system startup, while subsequent reports represent activity during the preceding interval.

Important Columns

ColumnSectionMeaning
rProcessesProcesses waiting to run on the CPU
bProcessesProcesses blocked while waiting for resources, often I/O
freeMemoryUnused memory
siSwapMemory swapped in from disk per second
soSwapMemory swapped out to disk per second
biI/OBlocks received from block devices per second
boI/OBlocks sent to block devices per second
usCPUCPU time spent running user-space code
syCPUCPU time spent running kernel code
idCPUPercentage of CPU time spent idle
waCPUPercentage of CPU time spent waiting for I/O

The exact units and some columns depend on the operating system and vmstat implementation.

Understanding CPU Idle Time

The id column shows the percentage of CPU time spent idle. A lower value means the CPU is spending more time doing work or waiting for I/O.

Idle (id)Interpretation
99CPU is mostly idle
80CPU is idle 80% of the time
10CPU is busy for most of the interval
5CPU has very little idle time

For example, if id is 10, the CPU is idle 10% of the time. The remaining 90% is accounted for by other CPU activity categories, including user, system, and I/O wait.

Don't rely on id alone. A high wa value may indicate that processes are spending substantial time waiting for I/O. A low id value suggests heavy CPU activity, but you should also check process-level usage and the workload before concluding that the CPU is the bottleneck.


Quick Reference

CommandPurpose
free -hCheck RAM and swap usage
df -hCheck filesystem capacity and available space
du -sh *Estimate the size of files and directories
topMonitor processes and resource usage interactively
vmstatView system performance statistics
vmstat 2Observe performance statistics at two-second intervals

Summary

  • Use free -h to understand memory usage, paying particular attention to the available column.
  • Use df -h to check filesystem capacity and du to investigate where space is being consumed.
  • Use top to identify processes consuming significant CPU or memory.
  • Use vmstat 2 to observe performance trends over time.
  • High CPU usage, low available memory, heavy swapping, and high I/O wait can all contribute to poor performance. Interpret these metrics together when troubleshooting Linux servers.

Written By: Muskan Garg

How is this guide?

Last updated on