1

I am attempting to write a generic,python script that memory profiles a target script, but I can't figure out how to get line-by-line memory stats from the script being run by subprocess.

# mem_profiler.py

import subprocess
import tracemalloc

    def profile(script_name, script_args):
        tracemalloc.start() snap1 = tracemalloc.take_snapshot()

        sp = subprocess.Popen["python", script_name] + script_args)
        sp.wait()
        snap2 = tracemalloc.take_snapshot()
 
        stats = snapshot2.compare_to(snapshot1, 'lineno')
  
        print("===[ Top 5 stats ]===") 
        for stat in stats[:5]:
          print(stat)

Running command python mem_profiler.py --target some_script.py --arg1 --arg2 gives this output:

===[ Top 5 stats ]===
/src/lib/python3.10/subprocess.py:1234: size=1320 B (+1320 B), count=3 (+3), average=440 B
/src/lib/python3.10/os.py:637: size=1248 B (+1248 B), count=3 (+3), average=416 B
/src/lib/python3.10/subprocess.py:971: size=928 B (+928 B), count=1 (+1), average=928 B
/src/mem_profiler.py:43: size=920 B (+920 B), count=2 (+2), average=460 B
/src/mem_profiler.py:27: size=912 B (+912 B), count=1 (+1), average=912 B

Obviously, tracemalloc is tracing mem_profiler.py rather than the target script; I want the above output, but for whatever script subprocess.py is running! Is this possible with tracemalloc and subroutines? If not, are there another PyPi packages I can use?

0 Answers0