I'm writing a script that parses certain information out of a series of Excel files. The logic to read the files and grab the data I need works fine. My problem is that I keep running out of memory. I'm running into what appears to be a memory leak that manifests itself when I open more files. I'm processing the files sequentially so I would expect that any memory allocated when I open a file would be freed when I open the next but that's not the case. I'm losing 1-2 Kb of memory with each new file. This hasn't been a problem with other ParseExcel scripts I've written since I was only working on 1 or 2 files. Now I'm dealing with over 800 files and it definitely is a problem.
Various searches have come up with alternative cell handling techniques to limit memory usage when parsing large files. I tried that just to make sure but it had no effect. The files I'm working with aren't that large, there's just a lot of them.
The pertinent sections of the code are listed below
#
# Subroutines
#
sub get_cell_val() {
my ($src_sheet, $src_row, $src_col) = @_;
if (defined $src_sheet->{Cells}[$src_row][$src_col]) {
$_ = $src_sheet->{Cells}[$src_row][$src_col]->Value;
} else {
$_ = '';
}
}
#
# Main Program
#
my @files = <*.xls>;
for my $file (@files) {
print "Opening DLR $file\n" if $DEBUG;
my $dlr_num = $file;
$dlr_num =~ s/.xls//;
print "DLR Number is $dlr_num\n" if $DEBUG;
my $src_book = new Spreadsheet::ParseExcel::Workbook->Parse($file)
+;
my $count = $src_book->{SheetCount};
print "Sheet Count = $count\n" if $DEBUG;
$count--;
my $src_sheet = $src_book->{Worksheet}[$count];
SHEET: for (my $row = 0 ; $row <= 100 ; $row++) {
for (my $col = 0 ; $col <= 5 ; $col++) {
my $cell_val = &get_cell_val($src_sheet,$row,$col);
# Check for disconnected circuits
if ($cell_val =~ /disco/i) {
print "$dlr_num has been disconnected\n" if $DEBUG;
last SHEET;
}
# Start looking for related Order Numbers
if ($cell_val =~ /Circuit Layout Data/i) {
print "Entering Circuit Layout Data\n" if $DEBUG;
while ($row <= 100) {
my $cell_val2 = &get_cell_val($src_sheet,$row,2);
if ($cell_val2 =~ /DWDM/i) {
$cell_val2 =~ s/^.*(DWDM....).*$/$1/i;
print "We have a WINNER!!! - DLR # $dlr_num re
+ferences $cell_val2\n\n" if $DEBUG;
}
$row++;
}
}
}
}
}
Does anyone have a suggested work-around or fix for this? Or is there a problem with my code that I'm not seeing?
Thanks,
Jack
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.