Solution to PHPExcel memory leak problem, phpexcel leak_PHP tutorial

WBOY
Release: 2016-07-13 10:09:20
Original
1146 people have browsed it

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().

www.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...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template