The world’s leading publication for data science, AI, and ML professionals.

Visualizing CPU, Memory, And GPU Utilities with Python

Analyzing CPU, memory usage, and GPU components for monitoring your PC and deep learning projects

Photo by Patrick Pahlke on Unsplash
Photo by Patrick Pahlke on Unsplash

When you are indulged in Programming, you are trying to compute, debug, and code to achieve the desired task. When the project is more complex, there are several situations where you will open multiple tabs for research, multiple Python files for debugging your code, and various data elements for analysis. But "Boom" – there is suddenly a crash.

While you have been at it coding various projects, you forgot to keep a check on the resources utilized, which may result in loss of unsaved data or worse. CPU and memory usage are crucial parts of a computer system. GPUs are essential elements to monitor for deep learning projects.

In this article, we will look at a Python project that will visualize and display all the primary parameters that we are interested in analyzing. We will use specific tools for making this process of analysis more straightforward as such resources are useful for system monitoring, profiling, restricting process resources, and process management.

To find the best qualities of building your own PC within your budget range, I would recommend checking out one of my previous guides from this link. If you are interested in understanding the complexities and the requirements of GPU for your PC builds, I would suggest checking out the article provided below, which covers an in-depth guide on exploring this concept.

Do you Really Need A GPU For Deep Learning?


Understanding the necessary Library Requirements:

Photo by Luisa Brimble on Unsplash
Photo by Luisa Brimble on Unsplash

In this section, we will understand some of the basic library requirements that will be required to complete this project successfully. Most of these libraries can be installed with a simple pip command and do not require any additional steps. Let us look at the first two installations that you will need to read and analyze the CPU, memory, and GPU components.

pip install psutil
pip install GPUtil

If you are having issues with the second command during installation, I would recommend the following pip install command instead and following this GitHub link by the authors for further reference.

pip install gputil

With these simple library installations, the users can already acquire all the essential information that will be required for this project. Firstly, let us import both libraries through which we can test if they are installed correctly and working as anticipated. Once the libraries are imported, we can view the rate of performance of the CPU and the memory usage as we use our PC.

The same step can be followed for analyzing the GPU performance as well for monitoring how much memory is consumed by your GPU. You can notice that at the start that the values printed are quite constant. However, when you move the cursor or proceed to do some small activities on your system, there is a spike in the details printed. Below is the code block for testing the following process.

# Importing the essential libraries
import psutil
import GPUtil
# Testing the psutil library for both CPU and RAM performance details
print(psutil.cpu_percent())
print(psutil.virtual_memory().percent)
# Testing the GPUtil library for both GPU performance details
GPUtil.showUtilization()

In the next step, we will look at some of the visualizations that we might find useful for developing our project with ease. We will import the numpy and matplotlib libraries through which we can create our own random data elements and plot the information accordingly. Below is the code snippet for testing and performing the following action.

# Importing the numpy and visualization library
import numpy as np
import matplotlib.pyplot as plt
# Plotting the axis
plt.axis([0, 10, 0, 1])
# Creating a random scatter plot
for i in range(10):
    y = np.random.random()
    plt.scatter(i, y)
    plt.pause(0.05)
plt.show()

In the above code snippet, I have provided a glimpse of what we can achieve with visualization techniques. If the viewers are further interested in visualizations, which is an integral part of data science, I would recommend checking out the following article provided below. It covers eight of the most critical visualization techniques that must be considered for any project that you plan to build for data analysis.

8 Best Visualizations To Consider For Your Data Science Projects!


Developing your own memory tracker with Python:

Image By Author
Image By Author

Now that we have understood all the library requirements and how to use them effectively, we can start developing our project. Our objective is to obtain a scatter plot, as shown in the above image, through which we can continuously monitor our resources accordingly. When it reaches its higher limits during more demanding applications like performing deep learning projects, gaming, 3-D rendering, or other similar tasks, we can know how to handle such situations.

For developing the project, we will import all the previously discussed libraries into a single Python file. Once we finish importing these libraries, we can proceed to create a somewhat infinite for loop that will run for a long time as long as your computer is on. In this loop, we will obtain the CPU utilization percentage, memory usage, and GPU information. Below is the complete code block for plotting the information for all the essential details that we plan to monitor for various system tasks.

# Importing the required libraries
import psutil
import GPUtil
import numpy as np
import matplotlib.pyplot as plt
# Creating an almost infinite for loop to monitor the details continuously
for i in range(100000000):
    # Obtaining all the essential details
    cpu_usage = psutil.cpu_percent()
    mem_usage = psutil.virtual_memory().percent
    print(cpu_usage)
    print(mem_usage)
    # Creating the scatter plot
    plt.scatter(i, cpu_usage, color = "red")
    plt.scatter(i, mem_usage, color = "blue")
    plt.legend(["CPU", "Memory"], loc ="lower right")
    plt.pause(0.05)
    # Obtaining the GPU details
    GPUtil.showUtilization()
# Plotting the information
plt.show()

I have created plots for only the first two components as I will not really be utilizing my GPU for my current procedures. The users can feel free to create their own methodologies and plots for GPU and other features as well if they deem it necessary. Once the program is completed, the best way to run it is in a command prompt (or terminal) by opening it in the working directory and running the Python file. Once the program is run, you can constantly and consistently monitor the necessary information accordingly.


Conclusion:

Photo by Umberto on Unsplash
Photo by Umberto on Unsplash

"Imagination is more important than knowledge. For knowledge is limited, whereas imagination embraces the entire world, stimulating progress, giving birth to evolution." — Albert Einstein

When working on numerous tasks and projects with a large number of files, folders, and tabs open on your local system, it is always a good idea to continuously monitor the consumption of your resources to ensure that they do not exceed their limits. While these issues are uncommon in higher-end systems or workstations, it can be considered mandatory for most average laptop and PC users to confirm the best utility of their resources.

In this article, we understood some of the basic library requirements and how to utilize them to analyze CPU consumption, memory usage, and GPU statistics. We also looked at some of the basic visualization concepts to plot these memory utilities accordingly. Finally, we combined all these elements together to develop our memory tracker with Python for monitoring your PC and deep learning projects.

If you want to get notified about my articles as soon as they go up, check out the following link to subscribe for email recommendations. If you wish to support other authors and me, then subscribe to the below link.

Join Medium with my referral link – Bharath K

If you have any queries related to the various points stated in this article, then feel free to let me know in the comments below. I will try to get back to you with a response as soon as possible. As promised in my previous article, I will try to deliver three to five articles each month!

Check out some of my other articles in relation to the topic covered in this piece that you might also enjoy reading!

Build Highly Interactive Projects with Jupyter Notebooks

Best Seaborn Visualizations for Data Science

7 Python Programming Tips To Improve Your Productivity

Thank you all for sticking on till the end. I hope all of you enjoyed reading the article. Wish you all a wonderful day!


Related Articles