Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to get current CPU and RAM usage in Python?
Next article icon

How to get current CPU and RAM usage in Python?

Last Updated : 13 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Getting the current CPU and RAM usage in Python involves retrieving real-time information about how much processing power and memory your system is using at any given moment. For example, knowing that your CPU is at 18% usage and your RAM is 40% utilized can help monitor system performance or optimize your applications. Let’s explore different methods to do this efficiently.

Using psutil.virtual_memory()

This method uses the psutil library to get real-time CPU and RAM usage. It measures CPU utilization over a short interval and reports detailed RAM statistics like the percentage used and the actual memory consumed. It works well across different operating systems and is both accurate and easy to use.

Python
import psutil print("CPU usage (%):", psutil.cpu_percent(interval=1))  ram = psutil.virtual_memory() print("RAM usage (%):", ram.percent) print("RAM used (GB):", round(ram.used / 1e9, 2)) 

Output

CPU usage (%): 18.1
RAM usage (%): 87.4
RAM used (GB): 7.21

Explanation: psutil measures CPU usage over one second and retrieves memory details, printing RAM usage percentage and used RAM in gigabytes rounded to two decimals.

Using psutil.getloadavg()

By dividing the load by the number of CPU cores, you get a percentage estimate of CPU usage. It’s useful for understanding overall system workload trends rather than moment-to-moment CPU activity.

Python
import os import psutil  load1, load5, load15 = psutil.getloadavg() cpu_usage = (load1 / os.cpu_count()) * 100 print(round(cpu_usage, 2), "%") 

Output

0.0 %

Explanation: psutil and os get the 1, 5 and 15-minute load averages, then calculate CPU usage by dividing the 1-minute load by CPU cores and converting it to a percentage.

Using psutil.Process().memory_info()

This approach focuses on monitoring how much memory the current Python process is using. It reports the memory usage in megabytes and is helpful when you want to profile or optimize your own program’s memory consumption.

Python
import os import psutil  process = psutil.Process(os.getpid()) ram_used = process.memory_info().rss / (1024 * 1024)  # in MB print(round(ram_used, 2)) 

Output

25.86

Explanation: psutil and os measure the current Python process’s memory by getting its PID, retrieving the resident set size (rss) in bytes, converting it to megabytes, and rounding to two decimals.

Using os.popen()

This method runs the Linux command free through Python’s os.popen() and parses its output to find RAM usage. It’s a quick way to get memory information on Linux systems but isn’t portable to other operating systems.

Python
import os  total, used, free = map(int, os.popen('free -t -m').readlines()[-1].split()[1:]) print(round((used / total) * 100, 2)) 

Output

52.34

Explanation: This code uses the os module to run the Linux free command, extracts total and used memory in MB, calculates used memory percentage and prints it rounded to two decimals.


Next Article
How to get current CPU and RAM usage in Python?

P

PranjalGoyal
Improve
Article Tags :
  • Python
  • python-utility
Practice Tags :
  • python

Similar Reads

    How to Get Current Date and Time using Python
    In this article, we will cover different methods for getting data and time using the DateTime module and time module in Python.Different ways to get Current Date and Time using PythonCurrent time using DateTime objectGet time using the time moduleGet Current Date and Time Using the Datetime ModuleIn
    6 min read
    How to use sys.argv in Python
    In Python, sys.argv is used to handle command-line arguments passed to a script during execution. These arguments are stored in a list-like object, where the first element (sys.argv[0]) is the name of the script itself and the rest are arguments provided by the user.This feature is helpful when you
    3 min read
    How to get value from address in Python ?
    In this article, we will discuss how to get the value from the address in Python. First, we have to calculate the memory address of the variable or python object which can be done by using the id() function. Syntax: id(python_object) where, python_object is any python variable or data structure like
    4 min read
    Python | How to put limits on Memory and CPU Usage
    This article aims to show how to put limits on the memory or CPU use of a program running. Well to do so, Resource module can be used and thus both the task can be performed very well as shown in the code given below: Code #1 : Restrict CPU timePython3 # importing libraries import signal import reso
    2 min read
    Get Bit Coin price in real time using Python
    In this article we will see how we can get the current price of the bit coin. Bitcoin is a cryptocurrency. It is a decentralized digital currency without a central bank or single administrator that can be sent from user to user on the peer-to-peer bitcoin network without the need for intermediaries.
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences