Unless your machine has an unusually small amount of memory (<1GB) it is quite hard to see why you would be running out of memory on a 400MB file.

There are some anomolies in your code (you calculate $buf_size but never use it; you use & on sub calls which you shouldn't), but nothing that stands out as being the cause of the problem.

You are passing a string containing the whole file into to_ascii() and then copying it; and then passing the modified copy back and then copying it again; which can be alleviated by using references as below. Try that and see how you get on:

#!/bin/perl # File: get-count.pl # # Perl script to locate the trailer record of a Fiserv data file and r +eturn # the following information # # Usage: # perl get-count.pl INPUT_FILE # # Number of records per the trailer # Number of records based on the file size and record size # Record size. sub to_ascii { my( $s ) = @_; $$s =~ tr[\x40\x5a\x7f\x7b\x5b\x6c\x50\x7d\x4d\x5d\x5c\x4e\x6b\x60 +\x4b\x61\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\x7a\x5e\x4c\x7e\x6e\ +x6f\x7c\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xd1\xd2\xd3\xd4\xd5\xd6\x +d7\xd8\xd9\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xad\xe0\xbd\x5f\x6d\x79\x8 +1\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96\x97\x98\x99 +\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xc0\x6a\xd0\xa1] [\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\ +x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x +3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x5 +0\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61 +\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\ +x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e]; return; } sub file_info { my( $file ) = @_; open( FP2, "<$file" ) || die return( -1, -1, -1 ); binmode( FP2 ); my( $file_size ) = -s $file; my($s) = ''; seek( FP2, -$file_size, 2 ); read( FP2, $s, $file_size ); to_ascii( \$s ); if( $s =~ /999 (\d\d\d\d\d\d\d\d\d)( +$)/ ) { my( $rec_size ) = 4 + length( $1 ) + length( $2 ); my( $rec_count ) = $1 + 0; my( $rec_count_calc ) = ( $file_size / ($rec_size) ) - 1; return( $rec_count, $rec_size, $rec_count_calc ); } else { return(-1, -1, -1); } close(FP); } foreach $file ( @ARGV ) { my( $rec_count, $rec_size, $rec_count_calc ) = file_info( $file ); print "File: $file\nRecord Count: $rec_count\nComputed Record Coun +t: $rec_count_calc\nRecord Size:$rec_size\n"; }

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Perl Out of memory error by BrowserUk
in thread Perl Out of memory error by vdel2393

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.