Prerequisites:
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.
CPU Usage
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 library import psutil # Calling psutil.cpu_precent() for 4 seconds print ( '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 os import psutil # Getting loadover15 minutes load1, 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
RAM Usage
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 library import 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 usage print ( "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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.