PHPExcel class memory footprint

Topics: User Forum
Mar 6, 2012 at 11:10 PM

Hello everybody,

I'm using PHPExcel 1.7.6 under PHP 5.3 to read a 380 x 14 Excel2007 sheet, containing just numbers (no formulas or formatting). I've read some tips from Mark Baker to limit memory footprint on stackoverflow, and I managed to halve it.

Yet there's something that puzzles me: if I set a Read Filter that always returns false, memory occupation still goes up 9MB upon instantiating the (empty) PHPExcel object.

I was wondering if this is normal, or if I am missing something. Here's my code:

 

	class MyReadFilter implements PHPExcel_Reader_IReadFilter {
		public function readCell($column, $row, $worksheetName = '') {
			return false;
		}
	}

	$filterSubset = new MyReadFilter();
	
	$objReader = PHPExcel_IOFactory::createReader('Excel2007');
	$objReader->setLoadSheetsOnly('Export');
	$objReader->setReadDataOnly(true);
	$objReader->setReadFilter($filterSubset);
	
	$objPHPExcel = $objReader->load($name);

 

Is there anything I can do to reduce those 9MB? I just need to read excel files, never to write them. What about cutting "write" methods away from some classes? Any other ideas?

Thank you

Coordinator
Mar 6, 2012 at 11:43 PM

Certain features are always loaded, irrespective of read filters. For example, every defined style will always be loaded unless you have setReadDataOnly(true), whether they are actually used in the cells that you filter out or those that you retain. That's because styles are loaded first, without anything to tell us what cells they apply to: it's only when we load the cells themselves (based on the filter) that we can actually assign those styles to their cells... so even an empty workbook can still contain a lot of information.

Likewise, named ranges, printer settings, etc are always loaded, irrespective of the setReadDataOnly() setting, or of read filters.

Only classes that you actually use are loaded (that's the value of the autoloader) so if you don't ever write, none of the writer classes will ever be loaded.

Mar 7, 2012 at 12:16 AM

Hi Mark,

I see your point. As you can see from the code, I already setReadDataOnly(true) on the reader. I just want to be sure that there's nothing else (method calls I'm not aware of, tips & tricks) to reduce those 9MB.

Thanks

PS: PHPExcel is a nice and very useful project, thank you for the time your are spending on it.

Mar 7, 2012 at 3:33 PM

By the way, how can I release all of the classes memory (PHPExcel, reader, worksheet) after my toArray() call (I don't need those classes any more)?

Is this the correct way to do it?

	unset($objWorksheet);
	unset($objPHPExcel);
	unset($objReader);
	gc_collect_cycles();

Thank you
Stephen

Coordinator
Mar 7, 2012 at 5:40 PM

It's awkward to release everything cleanly because of complex parent/child/parent relationships between the objects, but use:

$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);

rather than

 

unset($objWorksheet);
unset($objPHPExcel);

 

The reader can be unset as soon as you've loaded the file to $objPHPExcel

 

Mar 8, 2012 at 4:13 PM

Thank you very much Mark.