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

前端 未结 1 899
花落未央
花落未央 2021-01-01 00:03

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 amo

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 00:26

    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)
    

    0 讨论(0)
提交回复
热议问题