Re: Working with very large text files



ytoledano@xxxxxxxxx wrote:
> Hi,
> I'm working on a server which writes text to large log files. The
> problem is that these files are 1 GB and when I open them with perl,
> perl tries to load the entire file into the memory and fails when XP is
> out of memory.
>
> I only need to use the text written in the last 5 minutes, which
> shouldn't take more then a couple of megs, but since I'm using XP, I
> can't use `tail`.
>
> What code doesn't make perl load the entire file into the memory?

Perl will only load the entire file into memory if you tell it to, by
slurping the entire thing, like:

my @lines = <FILE>;
foreach (@lines) {
process_line($_);
}

If you instead read the file line by line, and discard each one after
it's read, no more than one line will ever be in memory at once:

while (<FILE>) {
process_line($_);
}

Note that you *must* use while in the above, not foreach. Even though
they look the same, they do *not* operate the same internally.

If you're looking for just the last five lines of the file, you may
also be interested in modules such as Tie::File and File::Tail. (The
former is standard, the latter is available on CPAN)

Paul Lalli

.



Relevant Pages

  • Memory leak, DBI 1.55 + DBD::ODBC 1.13 + MS SQL 2000
    ... Recently I have bumped into a memory leak happening in DBI. ... My Perl build config: ... ActivePerl Build 819 ... 28671 Define PERL_NO_DEV_RANDOM on Windows ...
    (perl.dbi.users)
  • Re: why the perl docs suck
    ... allocate new memory, copy then free old? ... Perl getting to that complex computation. ... >> the granularity of a Perl push statment. ... >> you don't know that memory allocation is not contiguous! ...
    (comp.lang.perl.misc)
  • Re: Multiple "MY" Same Variable - Release Bytes?
    ... Perl do not truly release more memory ... > bytes but rather make that malloc (memory allotment) available for ... arenas, ...
    (comp.lang.perl.misc)
  • server load question
    ... the drives by having the log files (sequential read and ... writes) on the same RAID array. ... >It sometimes has a peak load of up to 455 ... >We are getting some peaks with up to 1700 memory ...
    (microsoft.public.sqlserver.server)
  • Re: Linked lists
    ... Does perl have this concept? ... Linked list helps in good memory management. ... In C you need linked lists because it is expensive and cumbersome to ... collection is called Reference Counting. ...
    (perl.beginners)