Sharing examples of solving PHPExcel memory leaks

*文
Release: 2023-03-18 11:54:01
Original
1871 people have browsed it

This article mainly introduces the solution to the memory leak problem of PHPExcel. This article first explains the reasons for the memory leak, and then gives the solution. Friends in need can refer to the following

Use PHPExcel to generate excel Documents consume 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 documents In the case of generation operation, all constructed object instances cannot be released in time before the end of the http request, thus causing a memory leak.
The solution is to add a method to the PHPExcel_Worksheet class:


 public function Destroy() {
     foreach($this->_cellCollection as $index => $dummy) {
         $this->_cellCollection[$index] = null;
     }
 }
Copy after login


and add a method to the PHPExcel class:

 public function Destroy() {
     foreach($this->_workSheetCollection as $index => $dummy) {
         $this->_workSheetCollection[$index]->Destroy();
         $this->_workSheetCollection[$index] = null;
     }
 }
Copy after login


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

Related reading:

##Thinkphp5+PHPExcel implements batch upload table data function_php example

Introduction to the method of freezing and locking the header in PHPExcel

##Sharing of the method of exporting Excel files by PHPExcel in the Yii2 framework

The above is the detailed content of Sharing examples of solving PHPExcel memory leaks. For more information, please follow other related articles on the PHP Chinese website!

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