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.
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
Then import it:
That single dependency is usually easier than writing separate Linux, macOS, and Windows command wrappers.
Get System-Wide CPU and RAM Usage
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:
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:
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 whenpsutilalready provides structured data. - Forgetting that percentages can behave differently on multi-core systems.
Summary
- '
psutilis the standard practical way to read CPU and RAM usage in Python.' - Use
psutil.cpu_percent()andpsutil.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
- How can I get Docker Linux container information from within the container itself?
- How can I get the CPU temperature?
- How can I grant eks cluster permission to aws sso user?
- How can I initialize a MySQL database with schema in a Docker container?
- How can I get dictionary key as variable directly in Python not by searching from value?
- How can I get list of values from dict?
- How can I keep a container running on Kubernetes?
- How can I keep a container running on Kubernetes?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.