SPUG: Question: memory usage tracer

Doug Beaver dougb at scalar.org
Mon Aug 21 16:06:20 CDT 2000

On Thu, Aug 17, 2000 at 04:42:48PM -0600, Aryeh Cody Sherr wrote:
> Can anyone point me towards a module that will profile a cgi written
> in perl's memory usage? It would be particularly useful if I could
> print out a snapshot of the size of all variables at a given exection
> time. I poked around CPAN for a while, and didn't see anything.

There really are no modules for this, but you might find some helpful
information in the perldebug page under the heading 'Debugging Perl
memory usage'.  perldoc perldebug for more info...

It's possible to get statistics from perl's malloc if you use that
instead of the system malloc, and you can also get allocation info from
the -DL switch if you compiled perl with -g.  I haven't used either
method, unfortunately.

Devel::Peek has a mstat() function that seems to let you dump the memory
statistics from perl's malloc at any given time, although you'll still
have to figure out a way to make sense from the data that you get back
from it.

I wonder if you could combine Devel::Symdump and Devel::Peek to make a
guesstimate of the memory usage of the different variables.  If you knew
that a SvPVIV struct took 40 bytes of memory plus the length of the
string it was pointing to, then you could look at this output from

SV = PVIV(0x81fdfd0) at 0x81e4980
  REFCNT = 1
  IV = 42
  PV = 0x820a040 "42"\0
  CUR = 2
  LEN = 3

and just add the LEN field to the size for an empty SvPVIV and determine
that it's probably 43 bytes for that variable.  This seems like a lot
more trouble than it's worth, but it would sure be cool.  ;-)

If you pass a second argument to Dump, it will print out that many
elements of the structure (otherwise it stops on 4 or 5).

Try creating something really hairy in the debugger and then do this:

Dump \%big_nasty_hash, 1E9

That will dump the hash and include nested output for everything, so if
you have a hash where the keys are strings and the values are integers,
Dump() will print all the information about the hash and also
information about each value in the hash and what it points to.  You
could then figure out that a 100 key hash with 10 character keys and
integers for values takes 9KB of memory (or whatever it takes).

I guess it's something to try if you get bored or the other two options
don't work out for you...


Smithers: I'm afraid we have a bad image, Sir.  Market research shows
          people see you as somewhat of an ogre.
   Burns: I ought to club them and eat their bones!

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For full traffic, use spug-list for LIST ; otherwise use spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/

More information about the spug-list mailing list