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 ?
free
in linux to check my memory.So the answer to your question "Why does Perl take up my RAM on printing a file?" is that Perl is not using the memory. The operating system's file cache is using the memory.
Your Perl script wrote a large file. It first goes into file cache in RAM, the operating system gets around to writing it to disk when it has time or when it runs out of RAM. It doesn't remove it from RAM unless you delete the file or it needs the RAM for something else.
File cache is not a bad thing and you should not worry about how much of it is using your RAM. When a program needs more memory, the OS can clear out the file cache very quickly and reuse the memory for your program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With