Check CPU and Memory with top and free
Learn to check CPU and memory using top and free — essential Linux commands for monitoring system health, with hands-on examples and troubleshooting.
Focus: check cpu and memory with top and free
Your server feels slow, but you can't explain why. Requests are timing out, memory usage is climbing, and the team wants answers. Without visibility into what your CPU and memory are actually doing, you're debugging blind. This lesson shows you exactly how to check CPU and memory with top and free — two commands that every Linux operator uses to spot bottlenecks in seconds.
The Problem This Lesson Solves
Imagine your application's performance degrades over time. The first instinct is to blame the code, but the real culprit might be a runaway process eating 100% CPU or a memory leak that's slowly starving your services.
Why you need this now:
- You can't tune what you can't measure.
- Modern Linux servers host multiple processes — guessing which one is slow is inefficient.
- top and free give you instant, system-wide visibility without installing anything.
This lesson is your starting point for telemetry — the practice of collecting and interpreting system metrics. Before you can implement sophisticated monitoring stacks, you need to be fluent in the basic tools that ship with every Linux distribution.
Pro tip: Even senior engineers rely on
topandfreeas their first line of defense when diagnosing performance issues. They're simple, fast, and universally available.
Core Concept / Mental Model
Think of your server as a busy kitchen. CPU is the chef — the faster and less loaded it is, the more orders get cooked. Memory (RAM) is the counter space — essential for keeping ingredients (data) ready to use.
topis like a live dashboard on the kitchen wall, showing every cook's productivity and how much counter space is being used.freeis a quick snapshot of only the counter space, telling you how much is free versus in use.
Together, they give you a complete picture of your system's health. Here's the key definitions:
- CPU usage: The percentage of time the processor spends executing instructions versus sitting idle. High usage means processes are competing for CPU time.
- Memory usage: How much RAM is currently allocated. This includes memory used by running processes, kernel buffers, and caches.
A common mental model is the checklist:
1. Look at overall CPU usage in top. If it's near 100%, you have a CPU-bound problem.
2. Then look at memory usage with free to see if you're running low.
3. Finally, use top's process list to pinpoint the worst offender.
How It Works Step by Step
Let's break down how these commands behave and why they matter.
Reading top — The Live Dashboard
When you run top, you get a real-time, interactive screen. Here’s what to note:
- Summary lines at the top show overall system stats:
-
load average— a quick gauge of recent CPU demand (1, 5, 15 minutes). -%Cpu(s)— idleid, userus, systemsyusage. -MiB Mem— total, used, free memory. - Process list below — every process sorted by CPU usage by default. Columns include:
-
PID— process ID. -%CPU— CPU time divided by wall time. -%MEM— process memory as a percentage of total RAM.
Understanding free — The Memory Snapshot
The free command shows memory in kilobytes (or human-readable with -h). There are three key columns:
- total — physical RAM installed.
- used — memory currently used by the OS and processes.
- available — how much memory can be handed to new processes without swapping.
Important distinction:
freeincludes memory used for caching as 'buff/cache'. This is not wasted — the kernel releases it when programs need more. So always look at theavailablecolumn.
The Workflow
- Run
topfirst to see live CPU and memory usage for the whole system. - Run
free -hnext to see a cleaner memory summary. - Cross-reference: if
topshows high memory usage but your processes use little, it's the cache. If a single process uses high%CPUand%MEM, that's your bottleneck.
Hands-On Walkthrough
Let's put theory into practice. In this tutorial, you'll check CPU and memory on your system step by step.
Step 1: Open a Terminal
Simply open your Linux terminal — no installation needed.
Step 2: Run free to Check Memory
Run the following command:
free -h
Expected output (values will vary):
total used free shared buff/cache available
Mem: 15Gi 2.1Gi 8.7Gi 245Mi 4.7Gi 12Gi
Swap: 2.0Gi 0B 2.0Gi
total— your RAM capacity.used— memory actively used.free— completely unused.buff/cache— used for caching.available— the number you care about.
Step 3: Run top to See CPU Live
top
You’ll see something like:
top - 10:15:32 up 5 days, 3:42, 1 user, load average: 0.08, 0.03, 0.01
Tasks: 120 total, 1 running, 119 sleeping, 0 stopped, 0 zombie
%Cpu(s): 2.5 us, 1.2 sy, 0.0 ni, 96.2 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 15358.8 total, 8900.5 free, 2145.3 used, 4312.9 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 12358.6 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 ubuntu 20 0 1.2g 215m 20m S 6.7 1.4 1:02.35 python3
5678 ubuntu 20 0 345m 28m 10m S 2.0 0.2 5:21.11 sshd
- Look at
%Cpu(s)—id(idle) is 96.2%, meaning the CPU is mostly free. - Process list shows
python3is using 6.7% CPU and 1.4% memory.
Step 4: Exit top
Press q to quit.
Step 5: Find the Top CPU Consumer
While in top, you can press Shift+P to sort by CPU, or Shift+M for memory. But a quick command-line one-liner can also work:
top -bn1 | head -20
This runs a single batch update so you can see the summary and top processes in a non-interactive way—useful for scripts.
Compare Options / When to Choose What
The table below compares top and free against each other and other common monitoring tools.
| Tool | CPU info | Memory info | Real-time | Process-level | Interactive |
|---|---|---|---|---|---|
top |
✅ | ✅ | ✅ | ✅ | ✅ |
free |
❌ | ✅ | ❌ (snapshot) | ❌ | ❌ |
htop |
✅ | ✅ | ✅ | ✅ | ✅ (better) |
vmstat |
✅ | ✅ | ✅ | ❌ | ❌ |
ps |
✅ | ✅ | ❌ (snapshot) | ✅ | ❌ |
When to choose what:
- Use top for a quick live overview, especially when you need to see processes.
- Use free for a clean memory-only summary.
- Use htop if you prefer nicer visuals and easier sorting — but it's not installed by default.
- Use ps when you want a one-time snapshot of all processes.
Pro tip:
top -bn1is perfect for scripting. You can parse its output withawkorgrepto alert on high CPU usage automatically.
Troubleshooting & Edge Cases
Common Errors and Their Fixes
- Symptom:
topshows 100% CPU but no single process is high. - Cause: Kernel threads or hardware interrupts.
-
Fix: Look at %sy (system) — if high, there may be an I/O issue; use
iostator checkdmesg. -
Symptom:
freeshows almost nofreememory, but the system is responsive. - Cause: Memory used for caching.
-
Fix: Check the
availablecolumn — if it's high, you're fine. -
Symptom: The
%CPUcolumn can be over 100%. -
Explanation: On multi-core systems, a process can use multiple cores;
topshows the total across them. So 200% means two full cores. -
Edge case: When memory is exhausted, Linux starts using swap, which is slow. You'll see
Swapinfreebecome used. This indicates a critical memory shortage.
What You Learned & What's Next
You've mastered the basics of checking CPU and memory with top and free. Specifically, you now know:
- How to read
top's summary and process list to identify CPU and memory hogs. - How to use
free -hto get a concise memory report and understand theavailablecolumn. - When to use
topvsfreevs other tools. - Common pitfalls like interpreting
%CPUcorrectly and understanding cache usage.
What's next: In the next lesson in this track, you'll build on this foundation by learning how to monitor system metrics over time with tools like vmstat or iostat, and then move toward centralized telemetry with Prometheus or Grafana. You’ll be able to connect these basic commands to automated alerting and long-term trend analysis.
Practice recap
Now open a terminal and run free -h on your system. Note the available memory. Then run top and sort processes by memory with Shift+M. Identify the top three memory consumers. Repeat after launching a memory-hungry application to see how the numbers change.
Common mistakes
- Confusing
freememory withavailablememory — always checkavailablebecause the kernel will free cached memory when needed. - Thinking
%CPUabove 100% is an error — it simply reflects usage across multiple cores. - Forgetting to run
top -bn1in scripts, which causes the command to hang in interactive mode. - Ignoring
swapusage — if swap is actively used, your system is memory-constrained and performance will suffer.
Variations
- Use
htopfor a more interactive and visually appealing process manager with tree view and mouse support. - Use
vmstatfor a one-liner that reports CPU, memory, and I/O statistics simultaneously in a compact format. - Use
pidstatto get per-process CPU and memory reports over time, ideal for spotting leaks in specific applications.
Real-world use cases
- Debugging a sudden CPU spike on a production web server by identifying the offending process with
top. - Checking if a Linux machine has enough free memory before scheduling a large data-processing job using
free -h. - Writing a cron job that runs
top -bn1and alerts you when CPU usage passes a threshold.
Key takeaways
- The Problem: Without CPU and memory visibility, performance issues are invisible.
- Mental Model: CPU is the chef, memory is counter space —
topis the live dashboard,freethe snapshot. - Step-by-Step: Run
free -hfor memory, thentopfor a live process overview. - Compare: Use
topfor interactive monitoring,freefor memory-only,htopfor nicer visuals. - Troubleshooting: Check
availablememory, interpret%CPUper-core, and watch swap usage. - Next: Use
vmstatand telemetry systems to monitor trends over time.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.