Ask coding questions

← Back to all posts
how to know usage of RAM,ROM,CPU
chinnupavan48 (0)

how to know that how much of RAM,ROM,CPU is in use for this script ?

Comments
hotnewtop
MocaCDeveloper (713)

Hi!

I believe there is the package psutil available.
You will have to install it via pip(or pip3, depending). It's a simple installation:
pip3 install psutil.

Then, within your Python code(wherever you need it), just simply import it! There is some great documentation over this, but the most basic use is:

import psutil

print(psutil.cpu_percent(4))

I will briefly go over the above program:

psutil.cpu_percent gets the system-wide CPU utilization as a percentage. The arguments are interval = None, percpu = False. When the interval is > 0.0, it will compare the system CPU times elapsed before and after the interval(I believe you should know what interval means).

However, in your use case, you will want to stick to keeping the interval as its default value, since you're wanting to calculate the total memory used within the Python script.

If you want a list of CPU percentages, set percpu to true.

You can do more, like getting the CPU frequency(psutil.cpu_freq), get the average system load(psutil.getloadavg), get statistics about the virtual memory on your system(psutil.virtual_memory) and so much more!

You can find more documentation over this here.

If you have any questions, just let me know!