Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Perl take up my memory (RAM) on printing a file?

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.

Am I checking my memory usage at the right place here ?

like image 797
kelvien Avatar asked Jan 31 '17 21:01

kelvien


1 Answers

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.

like image 59
Zan Lynx Avatar answered Oct 09 '22 01:10

Zan Lynx