How to Check CPU and Memory Usage on Linux
Diagnose performance issues by analyzing CPU load, memory usage, and running processes.
Before you start
Step-by-step instructions
1. Use top for real-time system overview
Start with top to see real-time CPU and memory usage. This is the fastest way to identify whether the system is under load.
top
Watch CPU usage, load average, and the process list. Sort by CPU or memory to find the heaviest processes.
2. Use htop for better visualization (if available)
htop provides a clearer and more interactive interface compared to top. It allows easier sorting and process management.
htop
If htop is not installed, you can install it or continue using top. Both provide the necessary information.
3. Check memory usage with free
Use free to quickly check total, used, and available memory. This helps you understand whether the system is under memory pressure.
free -h
Focus on available memory rather than used memory. Linux uses memory for caching, which is normal behavior.
4. Identify high-resource processes
Once you detect high usage, identify which processes are responsible. This is where troubleshooting becomes actionable.
ps aux --sort=-%cpu | head
This quickly shows the top CPU-consuming processes so you can investigate or optimize them.
5. Interpret the results before taking action
Do not immediately kill processes or restart services. First understand whether the load is expected, temporary, or caused by a specific issue.
A spike during backup or batch processing is normal. Continuous high usage with performance impact is what requires investigation.
Real example
A server becomes slow during peak hours. Using top, an administrator sees one process consistently using high CPU. Further inspection shows it is a poorly optimized query in a backend service. Fixing the query resolves the performance issue without needing to scale the server.
This demonstrates why identifying the correct process is more important than reacting to overall usage numbers.
Possible causes
- High traffic or workload spikes
- Inefficient application logic or queries
- Background jobs or batch processes
- Memory leaks in long-running services
- Insufficient system resources
Common mistakes
- Assuming high memory usage is always a problem
- Killing processes without understanding their role
- Ignoring load average and focusing only on CPU percentage
- Not checking historical or recurring patterns
- Restarting services instead of fixing root causes
Environment tips
- Use
topfor quick checks andhtopfor interactive analysis. - Always check
free -hto understand memory availability. - Use
ps aux --sort=-%cputo identify heavy processes quickly. - Combine CPU, memory, and load information before making decisions.