How to get current CPU and RAM usage in Python?

24 Jan, 2021
Prerequisites:
psutil 
os
CPU usage or utilization refers to the time taken by a computer to process some information. RAM usage or MAIN MEMORY UTILIZATION on the other hand refers to the amount of time RAM is used by a certain system at a particular time. Both of these can be retrieved using python.
Method 1: Using psutil
The function psutil.cpu_percent() provides the current system-wide CPU utilization in the form of a percentage. It takes a parameter which is the time interval (seconds). Since CPU utilization is calculated over a period of time it is recommended to provide a time interval.
Syntax:
cpu_percent(time_interval)
Example:
Python
# Importing the libraryimport psutil  # Calling psutil.cpu_precent() for 4 secondsprint('The CPU usage is: ', psutil.cpu_percent(4))
Output:
The CPU usage is:  2.4
Method 2: Using OS module
The psutil.getloadavg() provides the load information of the CPU in the form of a tuple. The psutil.getloadavg() runs in the background and results gets updated every 5 seconds. The os.cpu_count() returns the number of CPU in the system.
Example:
Python3
import osimport psutil  # Getting loadover15 minutesload1, load5, load15 = psutil.getloadavg()  cpu_usage = (load15/os.cpu_count()) * 100  print("The CPU usage is : ", cpu_usage)
Output:
The CPU usage is: 13.4
Method 1: Using psutil
The function psutil.virutal_memory()  returns a named tuple about system memory usage.  The third field in tuple represents the percentage use of the memory(RAM). It is calculated by (total – available)/total * 100 . 
The total fields in the output of function are:
total: total memory excluding swap
available: available memory for processes
percent: memory usage in per cent
used: the memory used
free: memory not used at and is readily available
Example:
Python
# Importing the libraryimport psutil  # Getting % usage of virtual_memory ( 3rd field)print('RAM memory % used:', psutil.virtual_memory()[2])
Output:
RAM memory % used: 76.9
Method 2: Using OS module
The os module is also useful for calculating the ram usage in the CPU. The os.popen() method with flags as input can provide the total, available and used memory. This method opens a pipe to or from command. The return value can be read or written depending on whether mode is ‘r’ or ‘w’.
Syntax:
os.popen(command[, mode[, bufsize]])
Example:
Python3
import os  # Getting all memory using os.popen()total_memory, used_memory, free_memory = map(    int, os.popen('free -t -m').readlines()[-1].split()[1:])  # Memory usageprint("RAM memory % used:", round((used_memory/total_memory) * 100, 2))
Output:
RAM memory % used: 17.55
Note: The os module method works with Linux system only due to free flag and system command specified for linux system.
Picked
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts