7

Are there any methods in (C)Python to inspect the process' current memory usage? In particular, I'd like to determine the high-water mark of memory usage in a testing script, but if necessary I don't mind periodically checking memory usage and calculating the high water mark for myself.

EDIT: I'm looking for either a pure-python solution, or something which works on OS X.

Nathan Craike
  • 5,031
  • 2
  • 24
  • 19
dcrosta
  • 26,009
  • 8
  • 71
  • 83
  • does calling subprocesses still qualify as 'pure python'? – tMC May 28 '11 at 01:51
  • that's fine... maybe my question has become, "what OS X command should I call to find out how much memory a particular process is using"? I guess the answer is `ps` or something like it? – dcrosta May 28 '11 at 01:54

4 Answers4

5

Have a look at my answer to this question. It uses the getrusage() function from the standard library's resource module, and works on Mac OS X.

Community
  • 1
  • 1
Nathan Craike
  • 5,031
  • 2
  • 24
  • 19
  • Also, this question is _kind of_ a duplicate of the question I linked to, except that question's accepted answer is Linux-only. – Nathan Craike Oct 06 '11 at 03:45
3

On Linux, you can inspect the /proc/self/status file:

VmPeak:     6784 kB
VmSize:     6784 kB
VmLck:         0 kB
VmHWM:       572 kB
VmRSS:       572 kB
VmData:      180 kB
VmStk:       136 kB
VmExe:        44 kB
VmLib:      1640 kB
VmPTE:        36 kB
VmSwap:        0 kB

Probably VmPeak is the line you are most interested in, but if you mmap(2) a gigabyte-sized file, you'll probably be accounted for over a gigabyte, even if you only use three or four pages from the file.

If you're aware of the limitations of checking memory via top(1) or ps(1) then you're probably good to go. If you're not aware of the limitations of checking memory use, then be sure to look into the meanings of the Virt, Res, and Shr columns in top(1) output. :)

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • Sorry, I should have been more clear -- looking for something either pure-Python, or which works on OS X. I'll edit the question. – dcrosta May 28 '11 at 01:27
1

you can use os.getpid() to get your current PID and then use that PID to find the process in the output of a subprocess calling top/free/ps etc.

i'm not an OSX/BSD expert, so im unsure of the flags to which command will give you memory usage by process

tMC
  • 18,105
  • 14
  • 62
  • 98
0

MacosX has vmmap and vmmap64 that you could use to access the information. I'm not python literate, but I imagine you should be able to open a pipe to a subprocess to read the output somehow.

EDIT: Some additional non-python information can be found here.

Community
  • 1
  • 1
clstrfsck
  • 14,715
  • 4
  • 44
  • 59