Why, when I launch a new process using subprocess.Popen , and request psutil.Process.memory_info(pid) to get the Virtual Memory Size process, I get> 20 megabytes? (in the script that is started using Popen , simple reading and output of stdin. Stdin passes the "script-parent" to it)

main.py

 from subprocess import Popen, PIPE, STDOUT import sys, psutil, shlex with Popen(shlex.split('python3 ./test.py'), stdin=PIPE, stdout=PIPE, stderr=STDOUT) as proc: main_stdin = sys.stdin.read() print('Writting main STDIN stream:', main_stdin) proc.stdin.write(bytes(main_stdin, 'UTF-8')) # передаем stdin процессу mem_info = psutil.Process(proc.pid).memory_info() print('RSS:', mem_info.rss/1048576) # байты в Мегабайты print('VMS:', mem_info.vms/1048576) sub_stdout = proc.communicate()[0].decode('UTF-8') print('Reading sub STDOUT: ', sub_stdout) 

test.py

 from sys import stdin print(stdin.read()) 

Ubuntu 14.04, Python 3.4

  • This 20 mb also includes the python itself. Want less memory - use C or assembler. - KoVadim
  • And how to determine how much memory python itself takes? Run through the empty script popen? Just the point is to determine how much memory the script is eating, not the python itself. Then you can just subtract. I understand correctly? - Nikita Lapkov
  • Yes, right. But what is the point of measuring how much the script eats, if it doesn’t start up without a python? You can make the simplest script and see how long it takes. And assume that this is the size of the "python". - KoVadim
  • For example, the program has created a large list. I need to track exactly this, without taking into account the very "python" - Nikita Lapkov
  • then in the script you need to get the size of the occupied memory at the very beginning. - KoVadim

1 answer 1

Responses from comments:


This 20 mb also includes the python itself. Want less memory - use C or assembler.

And how to determine how much memory python itself takes? Run through the empty script popen? Just the point is to determine how much memory the script is eating, not the python itself.

Yes, right. That's just what is the point of measuring how much the script eats, if it doesn’t start without python? You can make the simplest script and see how long it takes. And assume that this is the size of the "python".