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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100