jakito has asked for the wisdom of the Perl Monks concerning the following question:
I read data in a anonymous array reference to save memory.
In normal ways, people read file in:
sub read_file { my $url = shift; my $file_data; open my $fh, '<', $url; local $/; $file_data = <$fh>; show_size(); } show_size(); read_file('name1.txt'); read_file('name2.txt'); read_file('name3.txt'); read_file('name4.txt'); read_file('name5.txt'); show_size(); sub show_size { print `top -l 1 | grep perl | awk '{print "MEM="\$8 "\tRPRVT="\$30 +}'`; # Work on Mac OS X 10.10 }
Output:
MEM=984K+ RPRVT=N/A
MEM=2052K+ RPRVT=N/A
MEM=2056K+ RPRVT=N/A
MEM=2060K+ RPRVT=N/A
MEM=4900K+ RPRVT=N/A
MEM=34M+ RPRVT=N/A
MEM=34M+ RPRVT=N/A
scalar $file_data's data will not release until program come to end. That makes memory increase and increase.
So, I read file by this way:
sub read_file { my $url = shift; my $file_data = []; open my $fh, '<', $url; local $/; $file_data->[0] = <$fh>; show_size(); } show_size(); read_file('name1.txt'); read_file('name2.txt'); read_file('name3.txt'); read_file('name4.txt'); read_file('name5.txt'); show_size(); sub show_size { print `top -l 1 | grep perl | awk '{print "MEM="\$8 "\tRPRVT="\$30 +}'`; # Work on Mac OS X 10.10 }
Output:
MEM=1048K+ RPRVT=N/A
MEM=2084K+ RPRVT=N/A
MEM=2084K+ RPRVT=N/A
MEM=2084K+ RPRVT=N/A
MEM=4928K+ RPRVT=N/A
MEM=37M+ RPRVT=N/A
MEM=4932K+ RPRVT=N/A
Perl uses reference counting way to manage memory. When ending to the block, $file_data->[0] will automatically release.
But... Is it memory safe? Or Is there something wrong? I don't know all but it just looks great.
The script is written on Mac OS X 10.10.2 and be tested in Perl 5.18.2. Maybe you should modify this adapting for your system.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Is it a good way to read in arr_ref? (Updated)
by Athanasius (Cardinal) on Jan 31, 2016 at 07:51 UTC |