best way to discover this process's current memory usage, cross-platform?
Neal Norwitz
nnorwitz at gmail.com
Tue Nov 15 00:54:49 EST 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
necessary.
These techniques could apply to Windows with some caveats. If you are
interested in Windows, see:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnucmg/html/UCMGch09.asp
Can't think of anything fool-proof though.
HTH,
n
More information about the Python-list
mailing list