dtd793353 2014-07-30 09:32
浏览 186

找不到修复PHPExcel内存泄漏的方法

I am using the following method inside a loop in order to read an excel file in chunks (using a custom read filter so that I don't run out of memory):

public function chunkToArray($file, $startRow, $chunkSize)
{

    $file = __DIR__ . '/../../' . $file;

    $objReader = PHPExcel_IOFactory::createReader('Excel2007');

    $chunkFilter = new ExcelChunkReadFilter();

    $objReader->setReadFilter($chunkFilter);
    $chunkFilter->setRows($startRow, $chunkSize);

    $objPHPExcel = $objReader->load($file);

    $array = $objPHPExcel->getActiveSheet()->rangeToArray('A' . $startRow . ':AT' . ($startRow + $chunkSize - 1));

    $objPHPExcel->disconnectWorksheets();

    unset($objPHPExcel);
    $objPHPExcel = null;

    return $array;
}

Despite calling "disconnectWorksheets" and unsetting and nulling the php excel object, I'm still getting a memory leak on each loop iteration.

I have used echo memory_get_usage(true) to isolate that the memory usage is going up on each iteration on the line $objPHPExcel = $objReader->load($file);, but the memory is not being cleared again before the next iteration.

I'm really struggling to identify what's going on here. Any help greatly appreciated.

  • 写回答

1条回答 默认 最新

  • donglu2008 2016-12-30 09:35
    关注

    The PHPExcel library has a big problem with memory leaks so I advise you to switch to other libraries that are working with .xlsx files.

    Have a look at this answer to the question "Alternative for PHP_excel". You can use PHP_XLSXWriter or Spout.

    If you can't switch to another library then you can use the caching mechanism of PHPExcel. This answer can help you with that.

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建