I've been slurping text files into a string using a loop and parsing the text, but I noticed that the memory in windows is always as large as the largest file that has been slurped (i.e., it never drops back down) even if I undefine the string each loop. Is this a problem with windows, or is there a way to resolve this in perl? A (rare) few of the files are 100K+ so this causes problems. I've simplified to code and even in this simple case, the effect is there:
#!C:/Perl/bin -w
use File::Listing qw(parse_dir);
my $dir='c:/mydir/';
#open the directory and get filenames;
opendir(TEMP, $dir) || die("Cannot open directory");
@thefiles= readdir(TEMP);
closedir(TEMP);
$maxsize=0;
#cycle through each of the files;
foreach $file (@thefiles)
{
unless ( ($file eq ".") || ($file eq "..") )
{
$filesize = -s $dir.$file;
if ($filesize > $maxsize){$maxsize=$filesize}
print "$file - $maxsize - $filesize\n";
my $html='';
$slurpfile=$dir.$file;
open( my $fh, $slurpfile ) or die "couldn't open\n";
my $html = do { local( $/ ) ; <$fh> } ;
undef $html;
}
}
Basically, I open up the directory and get a list of every file in the directory. Next, each file is individually opened and passed as a string to $html. I immediately undefine the string and repeat the loop. I can't understand why the memory isn't freed up. It should actually be freed up in 3 places each loop, shouldn't it? (1) when I define $html as '' (2) when I slurp the contents of the next file to $html and (3) when I undefine $html.
As it cycles through the thousands of files, I can watch the running maximum filesize and the memory allocated to perl increase in tandem.
I need to slurp the file for various reasons. I wouldn't mind this leak, but I have to do millions of files and it slows things down considerably. Any suggestions?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.