Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Is there a way(maybe tool?) to know the amount of memory an array takes?

by PerlOnTheWay (Scribe)
on Oct 21, 2011 at 12:34 UTC ( #932880=perlquestion: print w/replies, xml ) Need Help??
PerlOnTheWay has asked for the wisdom of the Perl Monks concerning the following question:

my @a; $a[100]=1;#how much memory taken by @a so far? $a[1000]=1;#how much memory taken by @a so far? $a[10000]=1;#how much memory taken by @a so far? ...

Is that possible?

  • Comment on Is there a way(maybe tool?) to know the amount of memory an array takes?
  • Download Code

Replies are listed 'Best First'.
Re: Is there a way(maybe tool?) to know the amount of memory an array takes?
by derby (Abbot) on Oct 21, 2011 at 12:42 UTC
Re: Is there a way(maybe tool?) to know the amount of memory an array takes?
by zentara (Archbishop) on Oct 21, 2011 at 12:57 UTC
    If you just want a general guideline as to how much memory your script is using, in realtime, with a simple little Tk display, see linux memory leak monitor. It won't be as accurate as Devel::Size, but its alot easier, all you do is put "use MeM;" at the top of your script, and a little window will open in the lower right corner, showing the second by second memory usage of your script.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Is there a way(maybe tool?) to know the amount of memory an array takes?
by Anonymous Monk on Oct 22, 2011 at 02:41 UTC
    I've seen this done
    my $before = current_memory(); my $after = current_memory(); SomethingSomething(); print join "\n", ' Before ', format_memory($before), ' After ', format_memory($after), ' Diff ', format_memory($after - $before), '' ; sub format_memory { my ($self, $memory) = @_; 1 while $memory =~ s/^([-+]?\d+)(\d{3})/$1,$2/; return "$memory KB"; } sub current_memory { my $self = shift; my $out = `ps -o rss= -p $$`; $out =~ s/^\s*|\s*$//gs; $out; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://932880]
Approved by sundialsvc4
help
Chatterbox?
choroba waves
[Mj1234]: Hello Monks, I have installed Active perl 5.16 and 5.14 but I dont find Devel::GDB package when I try to install through ppm
[Discipulus]: i'v installed ubuntu 16.04lts and i'm quite disappointed: is deadly simmilar to new win osses where you cannot find even simple things...
[Corion]: Hi Discipulus ;)
[Corion]: Mj1234: Maybe Ubuntu didn't package Devel::GDB? In that case, consider using local::lib and install the module from CPAN
[Corion]: But a quick Google search turns up this page, which seems to suggest that it should be packaged
[Discipulus]: iirc active perl has repositories only for last 2 o 3 major release of perl Mj1234. I strongly suggest portable version of strawberry perl and use cpan client to install modules
[choroba]: Also, who needs GDB when there's hdb?

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2016-12-07 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (125 votes). Check out past polls.