PHPExcel memory leakage solution: phpexcel leakage. PHPExcel memory leakage solution: phpexcel leakage uses PHPExcel to generate excel documents, which consumes a lot of memory. sometimes you may need to use a loop to split the big data into PHPExcel memory leakage solution, phpexcel leakage
Using PHPExcel to generate excel documents consumes a lot of memory. sometimes you may need to use a loop to split big data into several small excel documents and save them to avoid memory depletion.
However, PHPExcel has circular references (it seems that this problem has not been solved in the latest version 1.6.5 ), if you repeatedly construct PHPExcel and PHPExcel_Writer_Excel5 object instances in an http request to generate multiple excel documents, all constructed object instances cannot be released in time before the http request ends, resulting in memory leakage.
The solution is to add the method in the PHPExcel_Worksheet class:
The code is as follows:
Public function Destroy (){
Foreach ($ this-> _ cellCollection as $ index => $ dummy ){
$ This-> _ cellCollection [$ index] = null;
}
}
Add the following methods to the PHPExcel class:
The code is as follows:
Public function Destroy (){
Foreach ($ this-> _ workSheetCollection as $ index => $ dummy ){
$ This-> _ workSheetCollection [$ index]-> Destroy ();
$ This-> _ workSheetCollection [$ index] = null;
}
}
Then, you can call PHPExcel: Destroy () explicitly in the location where the resource is to be recycled to handle the issue of circular reference. Note that the _ destruct () method is called only when the object is considered to be released. Therefore, the processing of loop reference cannot be placed in _ destruct.
Using PHPExcel to generate excel documents consumes a lot of memory. sometimes you may need to split big data through a loop...