PHPExcel memory leakage solution, phpexcel leakage _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.