kaatunut has asked for the wisdom of the Perl Monks concerning the following question:
sub readPlaque { my($opt,@files)=@_; local *FILE; for my $fn (@files) { open(FILE,$fn) or (!$$opt{quiet} and print STDERR "couldn't op +en $fn\n"),next; my %plaq; chomp($plaq{time}=<FILE>); $plaq{plr}={}; $plaq{pos}={}; while (<FILE>) { chomp; # rank name level exp created age if (/^(\d+) (\w+) (\d+) (\d+) (\d+) (\d+)$/) { $plaq{plr}{$2}=$plaq{pos}{$1}=[$1,$2,$3,$4,$5,$6]; #{ +match([qw(rank name level exp created age)],[$1,$2,$3,$4,$5,$6]) }; $c++; } else { print "syntax error in $fn line $. '$_'\n"; } } close(FILE); chomp($ts=ctime($plaq{time})); print "Read plaque ",scalar(@plaque),": $fn ($ts)\n"; push @plaque,{ %plaq }; } }
This function is called on 85 files, and $c is 43676 afterwards. According to top, the memory consumption difference between before and after is about 22 megabytes. Since @plaque is the only structure that survives function exit, and never does the function allocate huge amounts of memory and then release it, I'd have to suppose it's @plaque with its fluff and 43767 pieces of actual data that eats this 22 megabytes, which would yield about 500 bytes per element (of 6 scalars). This seems a tad... excessive, especially concerning the data in file has known boundaries.
What can I do to limit the memory usage? I'll be accessing the data, whole of it, frequently so I really, really would like to avoid going for Storable/FreezeThaw with its constant slow and cumbersome freezes and thaws.
P.S. The commented part with 'match' concerns a related issue. For convenience I'd really like to access elements by names like rank, exp and created instead of indexes, but that'd eat even more memory. 'match' is inverse function to keys+values, reconstructing a hash from them. I suppose I could make each plaque an object and access them with subs returning a reference, but that'd screw operator precedence up and look ugly...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: My scalars swell
by Fletch (Bishop) on Oct 20, 2001 at 18:46 UTC | |
by kaatunut (Scribe) on Oct 20, 2001 at 19:41 UTC | |
|
Re: My scalars swell
by clintp (Curate) on Oct 20, 2001 at 19:58 UTC | |
by kaatunut (Scribe) on Oct 21, 2001 at 20:08 UTC | |
by tilly (Archbishop) on Oct 22, 2001 at 00:34 UTC | |
|
Re: My scalars swell
by jackdied (Monk) on Oct 20, 2001 at 21:12 UTC | |
by clintp (Curate) on Oct 20, 2001 at 21:52 UTC | |
by jackdied (Monk) on Oct 21, 2001 at 01:11 UTC | |
|
Reduce the number of structures
by jackdied (Monk) on Oct 21, 2001 at 01:01 UTC |