Out of memory in PERL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Out of memory in PERL
# 1  
Old 06-15-2006
Out of memory in PERL

I dont know whether this is a common error but it seems my PERL program is running out of buffer memory. Inside a loop I am processing an array (reading elements and displaying in it--note: for every iteration of the loop the WHOLE array is being scanned once--...the array was created by reading a file in it). I notice that as the number of repitions of the loop increases some characters are progressively clipped off off the end of the array element (I am storing strings). So, when the loop runs the first time, I get the elements I stored in the array, but by the time its in the last iteration, the elements are noticeably clipped from the end. In fact if read the output properly, I find that the clipping is increasing progressively with each iteration, which is ridiculous!

Can someone explain what happened here?
# 2  
Old 06-15-2006
Perl rarely goes out of memory. Under normal circumstances, it is more likely that a badly written program using the improper algorithm is doing the trouble.

Can you post a minimal example as to how you loop things? If you can quote a complete example that is giving you bad performance then that will likely help us understand why your program does not perform well.
# 3  
Old 06-15-2006
sample code

open (INFILE,"file1");
my @all_PL=<INFILE>;

close INFILE;

open (INFILE,"file2");
my @all_PL_additional_details=<INFILE>;
close INFILE;

my ($pl_line,$group_name,$file,$sd_line,$sd_group_name,$sd_file,$var);
foreach $pl_line (@all_PL)
{
chop($pl_line);
($group_name,$file)=reverse(split (/:/,$pl_line));
open (PLFILE,">>$TEMP_DIR/$file");
print PLFILE "something\n";

foreach $sd_line (@all_PL_additional_details)
{ chop($sd_line);
($sd_group_name,$sd_file) = reverse(split (/:/,$sd_line));

if ($sd_group_name eq $group_name) {

open (SDMATRIX,"$TEMP_DIR/$sd_file");
my @sd_matrix=<SDMATRIX>;
close SDMATRIX;

my @writable_data=@sd_matrix[2-11];
print PLFILE "something\n";
foreach $var (@writable_data) { print PLFILE "$var";}
}

}


close SDFILE;
# 4  
Old 06-15-2006
1. Holding the entire content of a file in an array (a slurp) for a huge file is likely to be inefficient, or just consume lots of memory. Try to rewrite your script so that each line is read as is processed.

2. In the loop you are opening a lot of PLFILE but you are not closing any one of them. If you do a lot of I/Os you should try to close filehandles as early as they are not needed.

As for "clipping", I have not experienced any of such behaviour myself.
# 5  
Old 06-15-2006
thanks

Actually I rewrote the parts where I am assigning the contents of a file to an array ( now processing the contents with a while (<FILEHANDLE>) , but the error still occurs ), but I sure missed the part abt the open PLFILE handles- thanks!

And, yes, this is the first time I faced such an error too....apparently an error which didnt generate an error message, which is much more ridiculous.
# 6  
Old 07-17-2006
for future reference

Hi,
This thread was started when I found an error that I wrongly attributed to buffer overrun in PERL. I found the bug recently, and thought would mention it for future reference. The progressive clipping of strings is happening because I was using chop() when I read in a line, to drop the trailing new line character. e.g.
chop($line);

Although this works fine for most of the time, I didnt realise that it would fail miserably for the last line of the file, which being the last line, DOESNT have a trailing new line character. So the above function was actually clipping off a REAL character! And more the loop ran, the worse the condition became.

Instead, now I am using this (which I believe is far more safer):
$line==~tr /\n//d;

Needless to say this bug would have been a nightmare if i had released my code into production!

Thanks to cbkihong for following up with the previous posts.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Out of memory issue in perl

I am getting a out of memory issue while executing the perl program. Per version : /opt/acc_perl/lib/site_perl/5.14.2 Read in 54973 total records Read in 54973 table records from table. Out of memory! so the job get failed due to out of memory. need to get rid of the out of memory... (3 Replies)
Discussion started by: ramkumar15
3 Replies

2. Shell Programming and Scripting

Perl code Out of memory problem

Hi I am working with a 3.0 GB file and running the following script to process it. After running for 15 minutes, the script stops saying Out of memory or Killed. Can the experts suggest where my code can be improved to save memory? Thanks in advance. #! usr/bin/perl use warnings; open... (3 Replies)
Discussion started by: polsum
3 Replies

3. Shell Programming and Scripting

Out Of Memory in Perl

Hi , when one of the perl script is being executed i am getting the following error as out of memory, when i check memory utilisation it is showing as 60% . cp utilisation as 11%. swap is free for 19 gb. can anyone guide me to sort the above isssue.i want to know the root cause. (2 Replies)
Discussion started by: Ajoy
2 Replies

4. Shell Programming and Scripting

Perl & Sed command -- Out of Memory Error

Experts, We used to receive our source files with '~^' as row delimiter. This file contains 2500K records and two of the columns having value in HTML formats within the file. While running the below commands against the file, we are encountering out of memory, could you please help to... (3 Replies)
Discussion started by: srivijay81
3 Replies

5. Shell Programming and Scripting

Is this "Out of Memory!" error due to sort() of perl how to resolve it?

I am running a program written in perl script and it is stopped with "Out of memory!" error. This is very strange because at the time then the program is aborted, it only used 4GB RAM and there are still 30GB free physical memory left in the system. I check the perl script and found the program... (3 Replies)
Discussion started by: lilili07
3 Replies

6. Shell Programming and Scripting

User perl to get memory installed in a machine

I currently have a statistics gathering script i run on my Linux servers. One of the stat i gather is total memory in the machine. The script is all perl with the exception of gathering the memory for that i use the following command: $ram = (`cat /proc/meminfo | grep "MemTotal" | awk... (1 Reply)
Discussion started by: insania
1 Replies

7. Shell Programming and Scripting

bash/perl script that will use up all memory

Hi, Is there any known trick that will consume all of my memory and force my machine to swap? I'm scripting a monitor in perl that will alert me when available memory is dangerously low and system is swapping, and I would like to simulate the scenario. I am clueless how to make my system lack... (4 Replies)
Discussion started by: marcpascual
4 Replies

8. Shell Programming and Scripting

Perl - FileHandles that crash memory

I have a program that opens a file handle that reads a lot of logs, it uses globbing, for example ba* matches bananas.log, bank.log, etc. I have seven different names, each with several logs that I run to a grep pattern. I have subroutines for each match and a loop w/o args that processes this. ... (5 Replies)
Discussion started by: nj78
5 Replies

9. UNIX for Dummies Questions & Answers

perl memory leak detector

Could somebody give links for memory detector for perl? Thanks :) (4 Replies)
Discussion started by: matrixmadhan
4 Replies

10. UNIX for Advanced & Expert Users

Weakness in Perl CGI causes memory dump ??

I have discovered a curious phenomenon in GCI. I need some advice from someone far more adept. Is this a bug or potential security weakness? Context: Redhat 8.0 on xx86 (pentium 3) Apache 2.0, Perl 5.80 Background: I've been trying to harden a web application which accepts user uploaded... (0 Replies)
Discussion started by: andyj
0 Replies
Login or Register to Ask a Question