Below is my code snippet...

...simple job, open a text log file, read through it line by line and then close it.

I am actually trying to extract statistics from our mail logs, so the final code will count up the number of messages to from each address and store this in a hash to be output to CSV file at the end. However, that's getting beyond myself.

The problem is when I run this with a 46Mb file, my WinXp workstation uses up all 1GB of RAM + Swap and goes ape. This seems alot of memory usage to just read through a file, even if the machine caches the entire file in memory. With this code, I am not storing any of the data or manipulating it in any way!

Any ideas from the Perl Monks?

Is there a more efficient way of reading through files?

# process.pl # andy bingham # 14 April 2002 # open(INP, "log.txt") or die "cannot open input"; foreach $line (<INP>) { # read in each line of INP #processing code goes here } close(INP); # # DONE #

In reply to Memory use when reading a text file by abingham

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.