Home > php教程 > php手册 > body text

PHPExcel内存泄漏问题解决方法,phpexcel泄漏

WBOY
Release: 2016-06-13 09:16:42
Original
840 people have browsed it

PHPExcel内存泄漏问题解决方法,phpexcel泄漏

使用 PHPExcel 来生成 excel 文档是比较消耗内存的,有时候可能会需要通过一个循环来把大数据切分成若干个小的 excel 文档保存来避免内存耗尽。
然而 PHPExcel 存在 circular references 的情况(貌似在最新的 1.6.5 版本中仍然没有去解决这个问题),如果在一次 http 请求过程中反复多次构建 PHPExcel 及 PHPExcel_Writer_Excel5 对象实例来完成多个 excel 文档生成操作的话,所有被构建的对象实例都无法在 http 请求结束之前及时释放,从而造成内存泄漏。
解决办法是在 PHPExcel_Worksheet 类中增加方法:

复制代码 代码如下:


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


并在 PHPExcel 类中增加方法:

复制代码 代码如下:


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


然后在需要资源回收的地方显式的调用 PHPExcel::Destroy() 来处理循环引用的问题。注意 __destruct() 方法是在对象被认为可以被释放的时候才会被调用,所以循环引用的处理不能放到 __destruct() 来进行。
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!