dkhosla1 has asked for the wisdom of the Perl Monks concerning the following question:
- Reading a 640 MB file into an array causes perl/OS to allocate 4.5GB in memory. Seems very high!
- Reading the same file into the same array, but via reference, makes it jump to over 6GB.
The first is too high as it is but the second is even more confounding. Why woudl just using a reference cause a memory use jump by 33%! I used Memory::Usage to dump the mem use (validated against 'top'). Ideally my 640MB file would only use 640MB ... I must be doing something obviously stupid here but can't figure it out.
Test result - option 1 read into an array#!/usr/bin/perl -w use Memory::Usage; use vars qw ($muse); $muse = Memory::Usage->new(); my $name = "bigfile"; # file size: 640MB my $READIN = qw (<); my $EXT; my @data = (); my $d = \@data; die "Error opening file: $name" if ( !open $EXT, $READIN, $name ); $muse->record('Begin'); @data = <$EXT>; # option 1 #@$d = <$EXT>; # option 2 close($EXT); $muse->record('After file read'); $muse->dump();
Test result - option 2 read into a reference to an array$ perl test_read.pl time vsz ( diff) rss ( diff) shared ( diff) code ( diff) + data ( diff) 0 78300 ( 78300) 2120 ( 2120) 1412 ( 1412) 12 ( 12) + 1164 ( 1164) Begin 12 4538324 ( 4460024) 4373332 ( 4371212) 1428 ( 16) 12 +( 0) 4461188 ( 4460024) After file read
$ perl test_read.pl time vsz ( diff) rss ( diff) shared ( diff) code ( diff) + data ( diff) 0 78300 ( 78300) 2116 ( 2116) 1412 ( 1412) 12 ( 12) + 1164 ( 1164) Begin 17 6461328 ( 6383028) 6296400 ( 6294284) 1428 ( 16) 12 +( 0) 6384192 ( 6383028) After file read
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: memory use array vs ref to array
by NetWallah (Canon) on Sep 08, 2016 at 05:59 UTC | |
by hippo (Archbishop) on Sep 08, 2016 at 08:36 UTC | |
by dkhosla1 (Sexton) on Sep 21, 2016 at 03:21 UTC | |
|
Re: memory use array vs ref to array
by dasgar (Priest) on Sep 08, 2016 at 06:11 UTC | |
by Anonymous Monk on Sep 08, 2016 at 06:59 UTC | |
|
Re: memory use array vs ref to array
by kcott (Archbishop) on Sep 08, 2016 at 22:58 UTC | |
by dkhosla1 (Sexton) on Sep 17, 2016 at 13:41 UTC | |
by BrowserUk (Patriarch) on Sep 17, 2016 at 15:04 UTC | |
by choroba (Cardinal) on Sep 17, 2016 at 20:36 UTC | |
by BrowserUk (Patriarch) on Sep 17, 2016 at 21:41 UTC | |
| |
by dkhosla1 (Sexton) on Sep 21, 2016 at 04:02 UTC | |
by dkhosla1 (Sexton) on Sep 21, 2016 at 03:59 UTC | |
|
Re: memory use array vs ref to array
by shmem (Chancellor) on Sep 18, 2016 at 16:45 UTC |