best way to discover this process's current memory usage, cross-platform?
nnorwitz at gmail.com
Tue Nov 15 06:54:49 CET 2005
Alex Martelli wrote:
> So, I thought I'd turn to the "wisdom of crowds"... how would YOU guys
> go about adding to your automated regression tests one that checks that
> a certain memory leak has not recurred, as cross-platform as feasible?
> In particular, how would you code _memsize() "cross-platformly"? (I can
> easily use C rather than Python if needed, adding it as an auxiliary
> function for testing purposes to my existing extension).
If you are doing Unix, can you use getrusage(2)?
>>> import resource
>>> r = resource.getrusage(resource.RUSAGE_SELF)
>>> print r[2:5]
I get zeroes on my gentoo amd64 box. Not sure why. I thought maybe it
was Python, but C gives the same results.
Another possibiity is to call sbrk(0) which should return the top of
the heap. You could then return this value and check it. It requires
a tiny C module, but should be easy and work on most unixes. You can
determine direction heap grows by comparing it with id(0) which should
have been allocated early in the interpreters life.
I realize this isn't perfect as memory becomes fragmented, but might
work. Since 2.3 and beyond use pymalloc, fragmentation may not be much
of an issue. As memory is allocated in a big hunk, then doled out as
These techniques could apply to Windows with some caveats. If you are
interested in Windows, see:
Can't think of anything fool-proof though.
More information about the Python-list