Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Total Memory Size Used up by a Perl Script

by BrowserUk (Patriarch)
on Oct 08, 2005 at 09:31 UTC ( [id://498401]=note: print w/replies, xml ) Need Help??


in reply to Total Memory Size Used up by a Perl Script

Try using Devel::Size::total_size on %::;

perl> use Devel::Size qw[ total_size size ];; perl> print total_size( \%:: );; [warnings omitted] 264621 perl> @a = 'A'..'M';; perl> print total_size( \@a );; 446 perl> print total_size( \%:: );; [warnings omitted] 265437 perl> @b{ 1 .. 100000 } = 1..100000;; perl> print total_size( \%b );; 4813243 perl> print total_size( \%:: );; [warnings omitted] 6803729

As you can see, the numbers don't exactly add up, but the do give you a pretty good ballpark figure.

Now, if only I can work out how to supress that dratted warning.

Update: I found it: $Devel::Size::warn=0;. With that, you can get a rough idea of where the memory your script uses is being used:

perl> use Devel::Size qw[ total_size ];; perl> $Devel::Size::warn = 0;; perl> print $_, ':', total_size( \$::{$_} ) for keys %::;; / : 382 stderr : 289 SIG : 3705 , : 399 utf8:: : 4359 " : 307 _<c:/Perl/site/lib/auto/Devel/Size/Size.dll : 479 DynaLoader:: : 15131 Devel:: : 7292 strict:: : 2185 stdout : 289 ↕ : 262 | : 378 _<c:/Perl/lib/auto/Time/HiRes/HiRes.dll : 463 Mac:: : 1701 Regexp:: : 948 _<c:/Perl/lib/auto/Data/Dumper/Dumper.dll : 469 UNIVERSAL:: : 1872 overload:: : 10646 $ : 287 Data:: : 22260 - : 679 _<..\universal.c : 363 _<HiRes.c : 343 BEGIN : 287 ! : 402 IO:: : 943 ☼ : 394 total_size : 487 ↑ : 345 _ : 414 + : 679 Exporter:: : 16111 Internals:: : 3384 STDIN : 287 Config:: : 54023 warnings:: : 19489 DB:: : 850 Time:: : 10381 _<.\win32.c : 343 ▬ : 345 _<perllib.c : 343 cmpthese : 489 1 : 402 ↨ARNING_BITS : 400 CORE:: : 930 _<Size.c : 339 attributes:: : 962 stdin : 287 ARGV : 405 INC : 2499 timethis : 489 ENV : 6657 Scalar:: : 1810 ? : 395 vars:: : 1545 _<..\perlio.c : 351 XSLoader:: : 1247 B:: : 951 main:: : 264297 AutoLoader:: : 2800 VMS:: : 1224 Carp:: : 11075 Win32:: : 11257 PerlIO:: : 2370 0 : 431 : 562 _<..\xsutils.c : 355 @ : 950 Benchmark:: : 25689 STDOUT : 289 ] : 355 ↨ : 407 Dumper : 479 STDERR : 289 bytes:: : 2985 _<Dumper.c : 345 _<dl_win32.c : 330 <none>:: : 460

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Replies are listed 'Best First'.
Re^2: Total Memory Size Used up by a Perl Script
by BrowserUk (Patriarch) on Oct 08, 2005 at 16:33 UTC

    To answer a few questions asked by /msg.

    1. What is Perl>?

      It's a twenty-line script called p1.pl that is effectively similar to doing perl -de1 except is loads a few useful modules and accepts multiple lines of input until one is terminated with ;;

    2. What is the meaning of the double semi-colons?

      See above.

    3. Why the diff is so big between the two examples: total_size( \@a );; and total_size( \%:: );;

      %\:: is (effectively) the symbol table for whole script, including all modules loaded.

      When you do total_size( \%:: );;, it is accumulating the sizes of most* the code space, and all of the data space use by everything that makes up the script, inclduing that space used by all modules that are loaded, and some of the code and data space loaded (into every script) by Perl itself.

      *See the Devel::Size docs where it explains that the numbers for CVs (code structures) are not complete. Don't expect the accumulated sizes to add up exactly. It gives a good idea of where your script is consuming memory, without being exact.

      It will include the space consumed by any variables created by your program (@a in the example) and various other bits and peices including some of the code and data loaded by the Devel::Size module itself, and the various modules ( Carp, Exporter, XSLoader etc. ) that D::S causes to be loaded.

      The long list in the Update above, is the results of dumping the keys and total_sizes of %:: individually from within p1.pl, and shows amongst other things that it also loads Data::Dumper, and Benchmark.

      It also shows various global structures set up by Perl. Eg.

      • %ENV is using 5,557 bytes.
      • %SIG is using 3,705 bytes.
      • %INC is using 2,495 bytes.
      • @ARGV is using 405 bytes.
      • With a little thought it is possible to work out most of the others.For example, the character '↨' using 407 bytes is ascii 23:
        perl> print ord '↨';; 23

        which translates to $^W, and '↑' is 24, $^X which is the name of the script.

      • ...and so on.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://498401]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-20 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found