Solution to PHPExcel memory leak problem, phpexcel leak
Using PHPExcel to generate excel documents consumes more memory. Sometimes it may be necessary to divide the large data into several small excel documents through a loop and save them to avoid memory exhaustion.
However, PHPExcel has circular references (it seems that this problem has not been solved in the latest 1.6.5 version). If PHPExcel and PHPExcel_Writer_Excel5 object instances are repeatedly constructed multiple times during an http request to complete multiple excel document generation operations. , all constructed object instances cannot be released in time before the http request ends, causing a memory leak.
The solution is to add a method to the PHPExcel_Worksheet class:
Copy code The code is as follows:
public function Destroy() {
foreach($this->_cellCollection as $index => $dummy) {
$this->_cellCollection[$index] = null;
}
}
And add methods in the PHPExcel class:
Copy code The code is as follows:
public function Destroy() {
foreach($this->_workSheetCollection as $index => $dummy) {
$this->_workSheetCollection[$index]->Destroy();
$this->_workSheetCollection[$index] = null;
}
}
Then explicitly call PHPExcel::Destroy() where resource recycling is required to handle the circular reference problem. Note that the __destruct() method will only be called when the object is considered ready to be released, so the processing of circular references cannot be done in __destruct().
http://www.bkjia.com/PHPjc/946744.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/946744.htmlTechArticleSolution to PHPExcel memory leak problem, phpexcel leakage. Using PHPExcel to generate excel documents consumes more memory, and sometimes it may It will be necessary to split the big data through a loop...