Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory usage of a single process with psutil in python (in byte)

How to get the amount of memory which has been used by a single process in windows platform with psutil library? (I dont want to have the percentage , I want to know the amount in bytes)

We can use:

psutil.virtual_memory().used

To find the memory usage of the whole OS in bytes, but how about each single process?

Thanks,

like image 692
pafpaf Avatar asked Nov 20 '14 13:11

pafpaf


People also ask

What is the use of psutil virtual memory?

1) psutil.virtual_memory () – This function gives system memory usage in bytes. The sum of used and available may or may not be equal to total. In order to get details of free physical memory this function is used. total – total physical memory excluding swap.

What is the load in psutil?

The load represents the processes which are in a runnable state, either using the CPU or waiting to use the CPU (e.g. waiting for disk I/O). 1) psutil.virtual_memory () – This function gives system memory usage in bytes.

What is psutil in Python?

PsUtil is a Python package with functionality to easily obtain system utilization information. Giampaolo Rodola initially developed and currently maintains the PsUtil code. He has done so for over a decade now, meaning that the code base is quite mature and stable. He works on the code regularly in his public PsUtil GitHub repository.

How much memory does a Python process use?

We can get this information using the handy psutil library, checking the resident memory of the current process: With this particular measurement, we’re using 3083MB, or 3.08GB, and the difference from the array size is no doubt the memory used by the Python interpreter and the libraries we’ve imported.


1 Answers

Call memory_info_ex:

>>> import psutil
>>> p = psutil.Process()
>>> p.name()
'python.exe'

>>> _ = p.memory_info_ex()
>>> _.wset, _.pagefile
(11665408, 8499200)

The working set includes pages that are shared or shareable by other processes, so in the above example it's actually larger than the paging file commit charge.

There's also a simpler memory_info method. This returns rss and vms, which correspond to wset and pagefile.

>>> p.memory_info()
pmem(rss=11767808, vms=8589312)

For another example, let's map some shared memory.

>>> import mmap
>>> m = mmap.mmap(-1, 10000000)
>>> p.memory_info()            
pmem(rss=11792384, vms=8609792)

The mapped pages get demand-zero faulted into the working set.

>>> for i in range(0, len(m), 4096): m[i] = 0xaa
...
>>> p.memory_info()                             
pmem(rss=21807104, vms=8581120)

A private copy incurs a paging file commit charge:

>>> s = m[:]
>>> p.memory_info()
pmem(rss=31830016, vms=18604032)
like image 66
Eryk Sun Avatar answered Oct 19 '22 00:10

Eryk Sun