Chapter 4.5 - Processes and Resources
Learning Objectives
By the end of this chapter, you will be able to:
- Understand CPU usage by processes
- Understand memory usage
- Monitor resources with different tools
- Identify resource-intensive processes
- Limit resources
CPU Usage
CPU Measurement
# With ps
ps aux --sort=-%cpu | head
# With top
top
# Column %CPU
# With htop
htop
# CPU bars per process
CPU Time
# Total CPU time used
ps -eo pid,time,comm
# Format: HH:MM:SS
# Example: 01:23:45 = 1h 23min 45s
CPU Load (Load Average)
# View load
uptime
# Example: load average: 1.25, 0.85, 0.65
# 1 min, 5 min, 15 min
# Interpretation:
# < number of CPUs: OK
# = number of CPUs: Full utilization
# > number of CPUs: Overload
# Number of CPUs
nproc
lscpu | grep "^CPU(s)"
Memory Usage
Memory per Process
# With ps
ps aux --sort=-%mem | head
# Readable format
ps aux --sort=-%mem -o pid,%mem,rss,comm | head
# With top
top
# Column %MEM, RES (RSS)
# RSS = Resident Set Size (physical memory)
Types of Memory
# View details
cat /proc/PID/status | grep -i mem
# VSZ: Virtual Size (virtual memory)
# RSS: Resident Set Size (physical memory)
# Shared: Shared memory
Total Memory
# Overview
free -h
# Details
cat /proc/meminfo
# By process
ps aux --sort=-%mem | head -10
Monitoring Tools
vmstat - System Statistics
# Statistics every second
vmstat 1
# Important columns:
# r: Processes waiting
# b: Blocked processes
# swpd: Swap used
# free: Free memory
# si/so: Swap in/out
# us/sy/id: CPU user/system/idle
iostat - I/O Statistics
# Install if necessary
sudo apt install sysstat # Ubuntu/Debian
# I/O statistics
iostat -x 1
# Columns:
# %util: Disk utilization
# r/s, w/s: Reads/writes per second
sar - System Activity Reporter
# CPU history
sar -u
# Memory history
sar -r
# I/O history
sar -b
# In real-time
sar -u 1 10 # CPU, 1 second, 10 times
Identify Resource-Intensive Processes
Top 10 CPU
ps aux --sort=-%cpu | head -11 | tail -10
Top 10 Memory
ps aux --sort=-%mem | head -11 | tail -10
Processes by User
# CPU by user
ps aux | awk '{cpu[$1]+=$3} END {for (user in cpu) print user, cpu[user]"%"}'
# Memory by user
ps aux | awk '{mem[$1]+=$4} END {for (user in mem) print user, mem[user]"%"}'
Limit Resources
ulimit - Per Process Limits
# View limits
ulimit -a
# Limit virtual memory
ulimit -v 1000000 # 1GB in KB
# Limit processes
ulimit -u 100
# Limit open files
ulimit -n 1024
# Permanent: /etc/security/limits.conf
cgroups - Advanced Control
# View cgroups
systemd-cgtop
# Limit CPU
systemd-run --slice=my-slice --property=CPUQuota=50% command
# Limit memory
systemd-run --slice=my-slice --property=MemoryLimit=1G command
Summary
In this chapter, you learned:
CPU: %CPU, CPU time, load average
Memory: %MEM, RSS, VSZ, types of memory
Monitoring: vmstat, iostat, sar for statistics
Identification: ps with sorting for resource-intensive processes
Limits: ulimit, cgroups to control resources
Next Steps
Chapter 4.6: Jobs and Background Tasks
Module 5: Users and Permissions
Chapter created on: December 2024