Sorted and reversed on huge dict ?

Nick Craig-Wood nick at craig-wood.com
Sat Nov 4 05:30:09 EST 2006


Paul Rubin <http> wrote:
> > is there a good way to know how much ram is used directly from
> > python (or should i rely on 'top' and other unix command?
> 
>  I think try resource.getrusage()

That should work (under BSD anyway) but unfortunately doesn't under
linux :-(

>From the man page

   CONFORMING TO
       SVr4,  4.3BSD.   POSIX.1-2001 specifies getrusage(), but only specifies
       the fields ru_utime and ru_stime.

and

       The  above struct was taken from 4.3BSD Reno.  Not all fields are mean-
       ingful under Linux.  In linux 2.4 only the fields  ru_utime,  ru_stime,
       ru_minflt, and ru_majflt are maintained.  Since Linux 2.6, ru_nvcsw and
       ru_nivcsw are also maintained.

Ie none of the memory based ones are filled in.

This linux only code works though :-

   def memory_used():
       """Return the memory used by this process under linux in bytes"""
       return int(file("/proc/self/statm").read().split()[0]) * resource.getpagesize()

Eg

>>> import resource
>>> def memory_used():
...     return int(file("/proc/self/statm").read().split()[0]) * resource.getpagesize()
... 
>>> print memory_used()
4575232
>>> a=10000000*"x"
>>> print memory_used()
14577664
>>> 

If anyone knows a (unix) portable way of doing this I'd be interested!

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list