How can I find the total amount of resident memory used by a Python process and all its forked children?
I know that I can use psutil
, for example, to find the percentage of a available physical memory used by the current process like so:
import os
import psutil
current_process = psutil.Process(os.getpid())
mem = current_process.memory_percent()
But I'm looking for the total memory used by a process and its children, if it has any.
You can use the result from psutil.Process.children()
(or psutil.Process.get_children()
for older psutil versions) to get all child processes and iterate over them.
It could then look like:
import os
import psutil
current_process = psutil.Process(os.getpid())
mem = current_process.memory_percent()
for child in current_process.children(recursive=True):
mem += child.memory_percent()
This would sum the percentages of memory used by the main process, its children (forks) and any children's children (if you use recursive=True
). You can find this function in the current psutil docs or the old docs.
If you use an older version of psutil than 2 you have to use get_children()
instead of children()
.