bronto has asked for the wisdom of the Perl Monks concerning the following question:

Hello

I was looking for a module (or a Linux program, if such a module doesn't exist) that could tell me how much memory a perl script is eating. I am not interested in the memory occupation of any single variable, so Devel::Size seems not to be an option.

In other options, I would like to do

 perl -MMemory::Profiling::Module script.pl

and get an output about the memory occupation when script.pl ends

I took a look at http://search.cpan.org/modlist/Development_Support but I found nothing. I also saw this node, and I have a debugging-enabled perl, but I am unable to get the meaning of the output it produces

Can you help me anyhow?

Thanks in advance

Ciao!
--bronto


The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
--John M. Dlugosz

Replies are listed 'Best First'.
Re: memory profiling
by mirod (Canon) on Mar 24, 2004 at 14:44 UTC

    On Linux, I use the data found in /proc/$$/status. I just insert a call to the following function right before the exit. As far as I know Perl does not release the memeory it uses, so this is the maximum size used by the process.

    sub total_size { open( STATUS, "/proc/$$/status") or die "cannot find status info i +n /proc/$$/status: $!"; my( $size)= map { m{^VmData:\s+(\d+\s+[\w+])} } <STATUS>; close STATUS; return $size; }
Re: memory profiling
by borisz (Canon) on Mar 24, 2004 at 14:41 UTC
    valgrind can do this and a lot more. I recommend that, at least in Linux.
    boris@foo:~> valgrind perl ~/xxx.pl ==28372== Memcheck, a.k.a. Valgrind, a memory error detector for x86-l +inux. ==28372== Copyright (C) 2002, and GNU GPL'd, by Julian Seward. ==28372== Using valgrind-1.9.6, a program instrumentation system for x +86-linux. ==28372== Copyright (C) 2000-2002, and GNU GPL'd, by Julian Seward. ==28372== Estimated CPU clock rate is 1005 MHz ==28372== For more details, rerun with: -v ==28372== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 90 from + 2) ==28372== malloc/free: in use at exit: 600146 bytes in 13063 blocks. ==28372== malloc/free: 21262 allocs, 8199 frees, 1013245 bytes allocat +ed. ==28372== For a detailed leak analysis, rerun with: --leak-check=yes ==28372== For counts of detected errors, rerun with: -v
    Boris
Re: memory profiling
by adrianh (Chancellor) on Mar 24, 2004 at 13:32 UTC
    I was looking for a module (or a Linux program, if such a module doesn't exist) that could tell me how much memory a perl script is eating.

    You might want to take a look at GTop and Measuring the Memory of the Process for some pointers.

Re: memory profiling
by liz (Monsignor) on Mar 24, 2004 at 13:22 UTC
Re: memory profiling
by Aragorn (Curate) on Mar 24, 2004 at 13:09 UTC
    man ps and man top can probably help you with that :-)

    Arjen

Re: memory profiling
by tpederse (Sexton) on Oct 21, 2009 at 23:04 UTC
    I find myself with the same question as Bronto had back in 2004 - any progress on memory profiling? I really don't want to modify the code that needs to be profiled (it's fairly complex), and as Bronto suggests it would be nice to have a command line option that would produce a summary of memory use. I did try the -Dm option but that shows every allocation and doesn't really summarize anything.

    Any ideas out there?