I have the following code
<?php
ini_set('memory_limit','1600M');
ini_set('max_execution_time', 3000);
require("phpexcel/Classes/PHPExcel.php");
$inputFileName = 'testa.xlsx';
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
function convert($size)
{
$unit=array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
/** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */
class chunkReadFilter implements PHPExcel_Reader_IReadFilter
{
private $_startRow = 0;
private $_endRow = 0;
/** Set the list of rows that we want to read */
public function setRows($startRow, $chunkSize) {
$this->_startRow = $startRow;
$this->_endRow = $startRow + $chunkSize;
}
public function readCell($column, $row, $worksheetName = '') {
// Only read the heading row, and the rows that are configured in $this->_startRow and $this->_endRow
if (($row == 1) || ($row >= $this->_startRow && $row < $this->_endRow)){
return true;
}
return false;
}
}
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
echo '<hr />';
/** Define how many rows we want to read for each "chunk" **/
$chunkSize = 25;
/** Create a new Instance of our Read Filter **/
$chunkFilter = new chunkReadFilter();
/** Tell the Reader that we want to use the Read Filter that we've Instantiated **/
$objReader->setReadFilter($chunkFilter);
/** Loop to read our worksheet in "chunk size" blocks **/
/** $startRow is set to 2 initially because we always read the headings in row #1 **/
for ($startRow = 2; $startRow <= 100; $startRow += $chunkSize) {
/** Tell the Read Filter, the limits on which rows we want to read this iteration **/
$chunkFilter->setRows($startRow,$chunkSize);
/** Load only the rows that match our filter from $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load($inputFileName);
// Do some processing here
$sheetData = $objPHPExcel->getActiveSheet();
$highestRow = $sheetData->getHighestRow();
//$sheetData = $sheetData->toArray(null,true,true,true);
//var_dump($sheetData);
echo '<br /><br />';
echo convert(memory_get_peak_usage(true));
}
?>
and when run it outputs this response.
277 mb
294.5 mb
295.5 mb
296.75 mb
It reads 25 lines at a time and so on throughout the file. What I can't figure out is, why does the memory peak keep rising?
I know that a whole Excel file has to be read before it can processed but surely that should use the same amount of memory every time and therefore the memory usage shouldn't change very much over time. However it appears to be constantly on the rise and what I cannot figure out is why.