How do I monitor CPU and memory usage on Linux?
Quick Command-Line Checks
The top command shows a live view of CPU and memory usage per process. Press 'q' to quit. htop is a more colorful, interactive alternative that you may need to install.
For a snapshot, use free -h to see memory and swap usage in human-readable units. vmstat 1 gives a rolling summary of CPU, memory, and I/O every second.
- top – real-time process and resource view
- htop – enhanced interactive viewer
- free -h – memory and swap summary
- vmstat 1 – CPU, memory, I/O every second
- mpstat -P ALL 1 – per-CPU usage
Long-Term Monitoring
For historical data and alerting, install a monitoring stack. Prometheus with node_exporter is a popular open-source choice. It scrapes metrics and stores them for querying and graphing.
Commercial options like Datadog or New Relic offer agents that collect CPU and memory metrics with minimal setup. They provide dashboards and alerting out of the box.
- Prometheus + node_exporter – open-source
- Netdata – real-time, auto-dashboard
- Datadog/New Relic – SaaS with agents
- Grafana – visualize Prometheus data
What to Look For
High CPU usage might indicate a runaway process or insufficient resources. Consistently high memory usage with low free memory could mean a memory leak or need for more RAM.
Pay attention to load average (shown in top and uptime). A load average higher than the number of CPU cores suggests processes are waiting.
Common mistakes
- Relying only on top and not checking historical trends.
- Ignoring swap usage, which can slow down the system.
- Confusing CPU usage percentage with load average; they measure different things.
