Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2015-07-07 23:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls