Python
CPU usage
RAM usage
system monitoring
psutil

How can I get current CPU and RAM usage in Python?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

In Python, the standard practical answer for CPU and RAM usage is the psutil package. It gives you system-wide metrics, per-process metrics, memory details, and percentage summaries without forcing you to parse platform-specific shell commands.

The main thing to decide first is what you want to measure: the whole machine or just your current Python process. Those are different numbers and both are useful.

Install and Import psutil

bash
python -m pip install psutil

Then import it:

python
import psutil

That single dependency is usually easier than writing separate Linux, macOS, and Windows command wrappers.

Get System-Wide CPU and RAM Usage

python
1import psutil
2
3cpu_percent = psutil.cpu_percent(interval=1.0)
4memory = psutil.virtual_memory()
5
6print("CPU percent:", cpu_percent)
7print("RAM percent:", memory.percent)
8print("RAM used:", memory.used)
9print("RAM total:", memory.total)

cpu_percent(interval=1.0) samples CPU usage over roughly one second. That delay matters. Without an interval, the first call can be misleading because it has no prior measurement window to compare against.

virtual_memory() returns a structured object with fields such as:

  • 'total'
  • 'available'
  • 'used'
  • 'free'
  • 'percent'

Measure Only the Current Python Process

Sometimes system-wide usage is too broad. If you want to know what your Python script itself is consuming:

python
1import os
2import psutil
3
4process = psutil.Process(os.getpid())
5
6cpu_percent = process.cpu_percent(interval=1.0)
7memory_info = process.memory_info()
8
9print("Process CPU percent:", cpu_percent)
10print("Process RSS bytes:", memory_info.rss)
11print("Process VMS bytes:", memory_info.vms)

rss is usually the most interesting memory value for application monitoring because it represents resident memory currently held in RAM.

A Small Monitoring Loop

If you want repeated measurements:

python
1import os
2import psutil
3import time
4
5process = psutil.Process(os.getpid())
6
7for _ in range(5):
8    cpu = process.cpu_percent(interval=1.0)
9    ram_mb = process.memory_info().rss / (1024 * 1024)
10    print(f"cpu={cpu:.1f}% ram={ram_mb:.1f}MB")
11    time.sleep(1)

This is a simple foundation for debugging long-running scripts, worker processes, or training jobs.

Human-Readable Memory Output

Raw bytes are precise, but they are not pleasant to read. For quick diagnostics, convert memory values into MB or GB before printing them. That makes logs and dashboards much easier to scan, especially when you are comparing several runs side by side.

For automated monitoring, keep the raw bytes too. Human-readable values are better for logs, while raw integers are better for thresholds and machine comparisons.

CPU Percent Can Be Tricky

CPU percentages are easy to misread:

  • system CPU percent measures total machine usage
  • process CPU percent measures the process contribution
  • multi-core systems can make process percentages look larger than expected

For example, a busy process on a multi-core machine can report a value that reflects usage across cores rather than a simple single-core fraction.

That is why it is helpful to decide early whether you want:

  • a rough health metric
  • a per-process diagnostic signal
  • a long-term time series for monitoring

Common Pitfalls

  • Calling cpu_percent() once with no interval and treating the first number as meaningful.
  • Confusing process memory with total system memory.
  • Comparing raw bytes directly without converting to KB, MB, or GB for readability.
  • Using os.system("top") or other shell parsing when psutil already provides structured data.
  • Forgetting that percentages can behave differently on multi-core systems.

Summary

  • 'psutil is the standard practical way to read CPU and RAM usage in Python.'
  • Use psutil.cpu_percent() and psutil.virtual_memory() for system-wide metrics.
  • Use psutil.Process(os.getpid()) for per-process CPU and memory usage.
  • The first CPU sample can be misleading unless you use a real interval.
  • Decide whether you need system-level or process-level numbers before interpreting the results.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.