Linux System Monitoring Commands #
Review
- 2020/04/15
This guide covers essential Linux system monitoring commands for analyzing system performance and resource usage.
1. top - Process Activity Monitor #
top
(Time of Process)is a real-time system monitor that displays system summary information and a list of processes currently being managed by the Linux kernel.
Basic Usage #
top
Key Metrics #
CPU Usage:
%us
: User space CPU usage (without nice scheduling)%sy
: System space CPU usage (kernel processes)%ni
: User space CPU usage with nice scheduling%id
: Idle CPU%wa
: CPU waiting for I/O operations%hi
: Hardware interrupt handling%si
: Software interrupt handling%st
: CPU stolen by virtual machine
Memory Usage:
Mem
: Physical memory usageSwap
: Swap space usage
Interactive Commands #
P
: Sort by CPU usageM
: Sort by memory usageN
: Sort by process IDk
: Kill a processq
: Quit top1
: Toggle CPU core display
2. free - Memory Usage #
free
displays the total amount of free and used physical and swap memory in the system.
brew install procps
Basic Usage #
free -h # Human readable format
free -m # Display in megabytes
Key Metrics #
total
: Total installed memoryused
: Used memoryfree
: Unused memoryshared
: Memory used by tmpfsbuff/cache
: Memory used by buffers and cacheavailable
: Memory available for new applications
3. vmstat - Virtual Memory Statistics #
vmstat
reports information about processes, memory, paging, block IO, traps, and CPU activity.
Basic Usage #
vmstat 1 # Update every second
# for macOS
vm_stat -c 5 1
Key Metrics #
- Procs:
r
: Running processesb
: Blocked processes
- Memory:
swpd
: Used swap spacefree
: Free memorybuff
: Buffer memorycache
: Cache memory
- Swap:
si
: Swap inso
: Swap out
- IO:
bi
: Blocks inbo
: Blocks out
- System:
in
: Interrupts per secondcs
: Context switches per second
- CPU:
us
: User timesy
: System timeid
: Idle timewa
: I/O wait timest
: Stolen time
get free memory
vm_stat | perl -ne '/page size of (\d+)/ and $size=$1; /Pages free:\s+(\d+)/ and printf("Free Memory: %.2f MB\n", $1*$size/1048576)'
4. pmap - Process Memory Map #
pmap
reports the memory map of a process, showing the memory usage of each segment.
brew install pmap
Basic Usage #
pmap -x <PID> # Detailed memory map
pmap -d <PID> # Display device format
Example #
# View memory usage of process with PID 5647
pmap -d 5647
Key Information #
- Address space
- Permissions
- Offset
- Device
- Mapping name
- Size
- RSS (Resident Set Size)
- Dirty pages
- Referenced pages
- Anonymous pages
Best Practices #
Regular Monitoring:
- Use
top
for real-time monitoring - Schedule regular checks with
vmstat
- Monitor memory usage with
free
- Use
Troubleshooting:
- High CPU usage: Check
top
for process details - Memory issues: Use
free
andpmap
- I/O bottlenecks: Monitor with
vmstat
- High CPU usage: Check
Performance Optimization:
- Identify memory-hungry processes with
pmap
- Track system resource trends
- Monitor swap usage to prevent performance degradation
- Identify memory-hungry processes with
Remember to run these commands with appropriate permissions (usually as root or with sudo) for complete system information.