How to measure subprocess memory usage?

Viewed 85

I want to run a few subprocesses in Python and get the amount of memory usage of these processes. I need only memory usage of the child processes and not the Python program.

import subprocess

out = subprocess.check_output([...], shell=True)

// Get somehow the usage

Could you please describe me if there's a way to get it?

1 Answers

You can use psutil library which is cross platform so you can use it in many OS

import psutil,subprocess
proc = subprocess.Popen([...])

process = psutil.Process(proc.pid)
mem_info=process.memory_info()

Here the return values explanation from the psutil doc:

memory_info() :

Return a named tuple with variable fields depending on the platform representing memory information about the process. The “portable” fields available on all plaforms are rss and vms. All numbers are expressed in bytes.

rss:

aka “Resident Set Size”, this is the non-swapped physical memory a process has used. On UNIX it matches “top“‘s RES column). On Windows this is an alias for wset field and it matches “Mem Usage” column of taskmgr.exe.

vms:

aka “Virtual Memory Size”, this is the total amount of virtual memory used by the process. On UNIX it matches “top“‘s VIRT column. On Windows this is an alias for pagefile field and it matches “Mem Usage” “VM Size” column of taskmgr.exe.

shared:

(Linux) memory that could be potentially shared with other processes. This matches “top“‘s SHR column).

text (Linux, BSD):

aka TRS (text resident set) the amount of memory devoted to executable code. This matches “top“‘s CODE column).

data

(Linux, BSD): aka DRS (data resident set) the amount of physical memory devoted to other than executable code. It matches “top“‘s DATA column).

lib (Linux):

the memory used by shared libraries.

dirty (Linux):

the number of dirty pages. pfaults (macOS): number of page faults. pageins (macOS): number of actual pageins.

Related