Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get a "high water mark" of memory usage from Python?

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.

like image 260
dcrosta Avatar asked May 28 '11 01:05

dcrosta


2 Answers

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.

like image 176
Nathan Craike Avatar answered Sep 18 '22 15:09

Nathan Craike


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. :)

like image 37
sarnold Avatar answered Sep 20 '22 15:09

sarnold