This question arises when I notice that my script starts to pauses on and off when it's in a while loop writing content to a file. I first check my memory and see a trend that this happens when my RAM decreased to about 2-3%. I then look for my code block that consumes the RAM and figured that as the script keeps feeding content to a file, it grows in size which also takes the memory too. Here is the code that does the file creation :
open (my $fh, '>>', $filePath);
while (my $row = $sth->fetchrow_hashref) {
print $fh join ('|', $row->{name}, $row->{address}) "\n";
}
If I uncomment the print statement and run the script, my RAM will not decrease. So, I'm pretty sure that my memory RAM is taken by a FILEHANDLE/something else behind Perl that takes up the memory. This leads me to a question if there is a way to write a file from Perl script to a disk, instead of using the memory RAM ?
- I have tried flushing FILEHANDLE on each row, and it still did not work.
- One thing that is also really weird is that, after I terminate my script I look at my memory and it's still being taken by the files. And when I remove the files, it frees up my memory. I use
free
in linux to check my memory.