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

Hi!

I have a very simple perl hello-world script.
I use GTop utility to measure the memory taken by the script.
The results GTop provides confuse me.

When I run the script from the command line GTop says it consumes 7M.
However when I run the script under mod_perl it consumes 54M.
So much?!

Why script memory grows so much under mod_perl?
Or maybe I measure the memory in a wrong way?
How do you profile perl script memory?

Here are the script and its output (I have added commas manually to easily read the numbers)

1. Run from command-line perl simple.pl size: 7,282688 share: 2,027520 diff: 5,255168 2. Run under mod_perl size: 54,878208 share: 4,661248 diff: 50,216960 Script simple.pl #!/usr/bin/perl use strict; use warnings; use CGI (); my $cgi = CGI->new; print $cgi->header('text/plain'); use GTop; print "Hello, world!\n"; my $m = GTop->new->proc_mem($$); print "size: ".$m->size."\n"; print "share: ".$m->share."\n"; my $diff = $m->size - $m->share; print "diff: $diff\n";

Replies are listed 'Best First'.
Re: Can I compute memory taken by a perl script?
by BioLion (Curate) on Oct 22, 2009 at 10:54 UTC

    There was a recent node on a similar topic - memory profiling - maybe it has some useful informations for you. Super Search will probably turn up more info as well.

    Just a something something...
Re: Can I compute memory taken by a perl script?
by FloydATC (Deacon) on Oct 22, 2009 at 12:49 UTC
    You may also want to read a little about how mod_perl uses memory. In short, on a loaded web server (i.e. the script is executed simultaneously by all children/threads) the script is likely to use the same amount of memory but without mod_perl it will be read into memory, parsed and executed once for each request.

    The only difference is, mod_perl executes the same copy of the parsed script in memory in order to gain a significant performance increase.

    For scripts that only run occasionally or where performance isn't a factor, mod_perl is (probably) a waste of memory.

    The real benefit from mod_perl comes when running several scripts that all use the same modules. In this case you can pre-load those modules once and share them between the different scripts.

    -- Time flies when you don't know what you're doing