How to clear the memory after reading the excel

Topics: Developer Forum, Project Management Forum, User Forum
Dec 10, 2012 at 7:02 AM

I had read the file using :

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

$objPHPExcel = $objReader->load("test.xlsx");
$objWorksheet = $objPHPExcel->getActiveSheet();

 

which is of 2300 rows and 42 columns. I had done the reading part and now i have to read another file which is of almost same to compare both.

While reading the second file i am getting the memory issue. So i just want to clear the memory to read the second file.

Can anyone help me on this?

Dec 10, 2012 at 11:08 AM
Edited Dec 10, 2012 at 11:08 AM

I think this question is in the top 3 of phpexcel questions :)

As always, you have to disconnect worksheets from phpexcel object and then unset it.

For the next file you have to recreate the object

To disconnect and unset:

$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
Dec 12, 2012 at 5:09 AM

Thanks Massi,

    It was not working for me as i am not disconnecting the work sheets before unset command.

 

Dec 13, 2012 at 10:37 AM

it happened to all of us :)

Jul 4, 2013 at 11:17 PM
Edited Jul 4, 2013 at 11:18 PM
Hi, I was trying to do this method.

I found, that unsetting is still not working as we want.

Example of unseting $objPHPExcel
echo sizeofvar($objPHPExcel); // 30837904 (30M) code from http://stackoverflow.com/questions/2192657/how-to-determine-the-memory-footprint-size-of-a-variable
So example of unsetting:
echo memory_get_usage(true); // 75759616 (75M) - current memory usage befere unsetting
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
echo memory_get_usage(true); // 57147392 (57M) - after
So unset clear 18612224 (18M) of memory. But size of $objPHPExcel is 30837904 (30M)

So after unset in memmory is still 12225680 (12M) of data which we don't wanted.