views:

134

answers:

0

I have spawned a process using:

ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0]

I want to do 3 things:

1) Get the memory usage of the Python script before the call to subprocess.Popen

2) Get the memory usage of the Python script after the call to subprocess.Popen

3) Get the memory usage of the spawned ps process.

I have seen both http://stackoverflow.com/questions/110259/python-memory-profiler and http://stackoverflow.com/questions/552744/how-do-i-profile-memory-usage-in-python which can probably do #1 and #2. Will Heapy be appropriate for this?

Alternatively I have also found http://code.activestate.com/recipes/286222/ which, according to a comment, can also easily get the memory usage of the spawned process:

memory usage of child processes. You can get the memory usage of any process by using the proper process_status string: simply replace os.getpid() with the pid of the process. To get the pid of a spawned process, use the Popen3 class from the standard popen2 module.

(I'm guessing process_status string is actually a typo and refers to line 3 in that recipe:

_proc_status = '/proc/%d/status' % os.getpid()

The issue is getting the memory usage of the ps process because it (should) terminate immediately after calling Popen due to the use of the communicate() method.

How would you approach this problem?