best way to discover this process's current memory usage, cross-platform?

Alex Martelli aleax at mail.comcast.net
Tue Nov 15 11:48:25 EST 2005


MrJean1 <MrJean1 at gmail.com> wrote:

> My suggestion would also be to use sbrk() as it provides a high-water
> mark for the memory usage of the process.

That's definitely what I would have used in the '70s -- nowadays, alas,
it ain't that easy.

> Below is the function hiwm() I used on Linux (RedHat).  MacOS X and
> Unix versions are straigthforward.  Not sure about Windows.

The MacOSX version using sbrk is indeed straightforward, it just doesn't
work.  See my response to Neal's post and my little Python extension
module at http://www.aleax.it/Python/memtry.c -- on a Mac (OSX 10.4,
Python 2.4.1, gcc 4.1) sbrk(0) returns the same value as the process's
virtual memory consumption goes up and down (as revealed by ps).  As the
MacOSX's manpage says, "The brk and sbrk functions are historical
curiosities left over from earlier days before the advent of virtual
memory management."

Guess I'll now try the linux version you suggest, with mallinfo:

> #if _LINUX
> #include <malloc.h>
> 
> size_t hiwm (void) {
>     /*  info.arena - number of bytes allocated
>      *  info.hblkhd - size of the mmap'ed space
>      *  info.uordblks - number of bytes used (?)
>      */
>     struct mallinfo info = mallinfo();
>     size_t s = (size_t) info.arena + (size_t) info.hblkhd;
>     return (s);
> }

and see if and how it works.

I do wonder why both Linux and MacOSX "implemented" getrusage, which
would be the obviously right way to do it, as such a useless empty husk
(as far as memory consumption is concerned).  Ah well!-(


Alex



More information about the Python-list mailing list