[SciPy-dev] Reliable way to know memory consumption of functions/scripts/etc..

Francesc Altet faltet at carabos.com
Wed Jun 13 06:36:55 EDT 2007


El dc 13 de 06 del 2007 a les 18:25 +0900, en/na David Cournapeau va
escriure:
> In found in between the option COUNT_ALLOCS, which looks exactly like 
> what I want, but unfortunately, it crashes when importing numpy, and 
> this seems to be non trivial to fix (I stoped digging after half an hour).

Too bad :-(

> > Incidentally, I've some code that gives you the amount of memory that is
> > currently being used by the process in some point of the code, but this
> > is different from knowing the amount of memory taken between two points.
> > If you are interested on this, tell me (only works on linux, but it
> > should be feasible to port it to win).
> Well, I don't use windows, so I could use your code :)

Ok. I'm attaching a small module, called procstats.py, with the
corresponding code.  The usage is quite easy:

import procstats
import numpy

tref = procstats.show("Starting program")
a = numpy.arange(1e6)
t1 = procstats.show("After allocating array a", tref)
b = a*a
t2 = procstats.show("After computing array b", t1)
del a
procstats.show("After removing array a")
del b
procstats.show("After removing array b")

gives as output:

Memory usage: ******* Starting program *******
VmSize:   18624 kB      VmRSS:    5604 kB
VmData:    3868 kB      VmStk:     124 kB
VmExe:      860 kB      VmLib:   11760 kB
WallClock time: 0.013
Memory usage: ******* After allocating array a *******
VmSize:   26444 kB      VmRSS:   13432 kB
VmData:   11688 kB      VmStk:     124 kB
VmExe:      860 kB      VmLib:   11760 kB
WallClock time: 0.098
Memory usage: ******* After computing array b *******
VmSize:   34260 kB      VmRSS:   21248 kB
VmData:   19504 kB      VmStk:     124 kB
VmExe:      860 kB      VmLib:   11760 kB
WallClock time: 0.122
Memory usage: ******* After removing array a *******
VmSize:   26444 kB      VmRSS:   13432 kB
VmData:   11688 kB      VmStk:     124 kB
VmExe:      860 kB      VmLib:   11760 kB
WallClock time: 0.018
Memory usage: ******* After removing array b *******
VmSize:   18628 kB      VmRSS:    5616 kB
VmData:    3872 kB      VmStk:     124 kB
VmExe:      860 kB      VmLib:   11760 kB
WallClock time: 0.049

HTH,

-- 
Francesc Altet    |  Be careful about using the following code --
Carabos Coop. V.  |  I've only proven that it works, 
www.carabos.com   |  I haven't tested it. -- Donald Knuth
-------------- next part --------------
A non-text attachment was scrubbed...
Name: procstats.py
Type: text/x-python
Size: 1310 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/scipy-dev/attachments/20070613/d235da8c/attachment.py>


More information about the SciPy-Dev mailing list