Reputation: 143
I have a script which reads emails with Excel files attached.
I am using PHPExcel to parse these files.
The current problem I am having, is with an email which has five spreadsheets attached.
Each time the fifth file is loaded, PHP runs out of memory.
Initially I tried nullifying ($objPHPExcel = NULL;
) and unsetting (unset($objPHPExcel);
) the PHPExcel Object each time after I was through with
it. That didn't help.
I then tried adding garbage collection to the process (gc_enable(); $cycles_collected = gc_collect_cycles();
). That didn't help.
I printed the current php memory usage to my error log and I can see it grow with the loading of each Excel file into PHPExcel, I also can see that the memory usage does not decrease with each NULL
, unset
or gc_collect
.
Here is some of the sample output to my error_log right before and after PHP runs out of memory:
[17-Aug-2017 16:04:29 America/Chicago] [Blackouts] Current Memory Usage at beginning of script before loading Excel file: 85575176
[17-Aug-2017 16:04:32 America/Chicago] [Blackouts] Current Memory Usage after loading Excel file: 104474632
[17-Aug-2017 16:04:34 America/Chicago] [Blackouts] Current Memory Usage after parsing of Excel file is complete: 104480416
[17-Aug-2017 16:04:34 America/Chicago] [Blackouts] Current Memory Usage after PHPExcel object set to null: 104480536
[17-Aug-2017 16:04:34 America/Chicago] [Blackouts] Current Memory Usage after PHPExcel object is unset: 104480256
[17-Aug-2017 16:04:34 America/Chicago] [Blackouts] gc collected cycles: 0
[17-Aug-2017 16:04:34 America/Chicago] [Blackouts] Current Memory Usage after gc_collect_cycles: 104480416
[17-Aug-2017 16:04:34 America/Chicago] [Blackouts] script completed: /path/to/file/Schedule Week of 14 Aug 2017 - 21 Aug 2017.xlsx
[17-Aug-2017 16:04:34 America/Chicago] [Blackouts] Current Memory Usage at beginning of script before loading Excel file: 104484072
[17-Aug-2017 16:04:36 America/Chicago] [Blackouts] Current Memory Usage after loading Excel file: 122069128
[17-Aug-2017 16:04:38 America/Chicago] [Blackouts] Current Memory Usage after parsing of Excel file is complete: 122114480
[17-Aug-2017 16:04:38 America/Chicago] [Blackouts] Current Memory Usage after PHPExcel object set to null: 122114600
[17-Aug-2017 16:04:38 America/Chicago] [Blackouts] Current Memory Usage after PHPExcel object is unset: 122114320
[17-Aug-2017 16:04:38 America/Chicago] [Blackouts] gc collected cycles: 0
[17-Aug-2017 16:04:38 America/Chicago] [Blackouts] Current Memory Usage after gc_collect_cycles: 122114480
[17-Aug-2017 16:04:38 America/Chicago] [Blackouts] script completed: /path/to/file/Schedules Week of 14 Aug 2017 - 21 Aug 2017.xlsx
[17-Aug-2017 16:04:38 America/Chicago] [Blackouts] Current Memory Usage at beginning of script before loading Excel file: 122118192
[17-Aug-2017 16:04:39 America/Chicago] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted at Zend/zend_vm_execute.h:22207 (tried to allocate 72 bytes) in /mnt/nas_001_www/Classes/PHPExcel/Cell.php on line 551
Upvotes: 1
Views: 2597
Reputation: 212522
You cannot simply nullify or unset a PHPExcel object to remove it from memory, as explained in the documentation, because it contains cyclic references (the workbook contains a collection of worksheet objects, and each worksheet references the workbook; and similarly with worksheet and cell objects) that cannot simply be resolved by an unset. Instead, you need to break those references first
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel)
Upvotes: 7