memory leak or newbie mistake?

Sep 1, 2009 at 1:23 PM
Edited Sep 1, 2009 at 1:24 PM

I am trying to loop through spreadsheets in a particular directory to create an array that will later be used to create worksheets in a PHPExel written workbook.  The problem I have is that memory usage continues to grow as the loop is executed.  I am not running out of memory but it seems I have something missing or wrong in my code.  Any suggestions?

 

<?php

include 'PHPExcel.php';
include 'PHPExcel/IOFactory.php';


$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objReader->setReadDataOnly(true);

$directory = "/home/products/Estimating/Bronx Lebanon Hospital 09-218/BOM/";

$sheets = array();
if ($handle = opendir("$directory")) {
while (false !== ($file = readdir($handle))) {
if (strpos($file, ".xls")) {
$objPHPExcel = $objReader->load("$directory$file");
$sourceSheet = $objPHPExcel->setActiveSheetIndex(0);
$rows = $sourceSheet->getHighestRow();

for ($x=1;$x<=$rows; $x++) {
$col = $sourceSheet->getCell("M$x")->getValue();
$sheets[$col] = $col;
}
echo memory_get_usage() . "<br/>";
}
}
}
?>

 

With $objReader->setReadDataOnly(true); inserted memory usage is 
14935752
15377688
15818888
16309104
16750168
17191288
17660496
18119712
18560760
18931712
19391032
19832072
20203032
20858864
21229784
21689144
22059992
22448992
22908208
23349368
23791000
24232064
24673160
25114280
25485200
....

without much bigger obviously. So where did I go wrong?

Thanks,


Developer
Sep 2, 2009 at 1:06 AM

This is unfortunately a known problem. PHP itself is a part of the problem the way it handles recursive references. (Not sure whether PHP 5.3 offers a solution.)

See this thread for more info:
http://phpexcel.codeplex.com/Thread/View.aspx?ThreadId=18404

We will work on reducing PHP memory usage via a cache provider. I think it may solve the memory leak you are describing too.