Linux Performance Monitoring: Find High CPU and Memory Usage

By Tech Writer Linux
Master Linux performance monitoring with top, htop, ps, and other tools. Learn how to identify and kill processes causing high CPU and memory usage.

Essential Performance Monitoring Tools

When your Linux system slows down, you need to quickly identify what's consuming resources. This guide covers the essential tools and commands.

Real-Time Monitoring with top

top

# Sort by CPU (press Shift+P)
# Sort by Memory (press Shift+M)
# Kill process (press k, enter PID)
# Quit (press q)

Understanding top Output

  • PID - Process ID
  • %CPU - CPU usage percentage
  • %MEM - Memory usage percentage
  • TIME+ - Total CPU time used
  • COMMAND - Process name

Better Monitoring with htop

sudo apt install htop
htop

htop advantages:

  • Color-coded display
  • Mouse support
  • Visual CPU/memory bars
  • Tree view of processes
  • Easy process killing (F9)
  • Search (F3) and filter (F4)

Find Resource Hogs Quickly

Top CPU Consumers

ps aux --sort=-%cpu | head -10

Top Memory Consumers

ps aux --sort=-%mem | head -10

Process Tree

pstree -p
ps auxf  # Forest view

Kill Problematic Processes

Kill Commands

# Graceful termination
kill PID

# Force kill
kill -9 PID

# Kill by name
pkill firefox
killall chrome

# Kill all processes by user
pkill -u username

Signal Types

  • kill -15 PID - SIGTERM (graceful, allows cleanup)
  • kill -9 PID - SIGKILL (immediate, no cleanup)
  • kill -1 PID - SIGHUP (restart/reload config)

Monitor Specific Resources

Disk I/O

sudo iotop
iostat -x 1 5

Network Usage

sudo nethogs
sudo iftop
nload

Memory Details

free -h
vmstat 1
cat /proc/meminfo

System Load Average

uptime
# Load: 1.5, 1.2, 0.8 = 1min, 5min, 15min averages

# Acceptable load depends on CPU cores
nproc  # Shows number of CPUs
# Load of 1.0 per core is generally acceptable

Set Process Priorities

Nice Values

# Start process with lower priority
nice -n 10 command

# Change priority of running process
renice -n 10 -p PID

# Nice values: -20 (highest) to 19 (lowest)

CPU Affinity

# Pin process to specific CPU
taskset -c 0,1 command  # Use CPUs 0 and 1

Limit Resource Usage

Using systemd

# Edit service file
sudo systemctl edit service-name

# Add resource limits
[Service]
CPUQuota=50%
MemoryLimit=512M

Using cgroups

# Limit memory for process
cgcreate -g memory:/mygroup
echo 512M > /sys/fs/cgroup/memory/mygroup/memory.limit_in_bytes
cgexec -g memory:mygroup command

Automated Monitoring

Install Monitoring Tools

# Glances - comprehensive monitor
sudo apt install glances
glances

# Nmon - performance analyzer
sudo apt install nmon
nmon

Troubleshooting High Load

  1. Check current load: uptime
  2. Identify top processes: top or htop
  3. Check disk I/O: iostat -x 1
  4. Monitor network: sudo nethogs
  5. Review logs: journalctl -xe
  6. Kill or restart problematic services
  7. Consider system upgrades if persistent

Conclusion

Performance monitoring is essential for maintaining healthy Linux systems. Use top and htop for quick checks, ps for detailed analysis, and specialized tools like iotop and nethogs for specific resources. Regular monitoring helps you catch issues before they impact users.