【问题标题】:PHPExcel Memory UsagePHPExcel 内存使用情况
【发布时间】:2015-06-14 23:57:21
【问题描述】:

我有以下代码

<?php

ini_set('memory_limit','1600M');
ini_set('max_execution_time', 3000);

require("phpexcel/Classes/PHPExcel.php");


$inputFileName = 'testa.xlsx';

$inputFileType = PHPExcel_IOFactory::identify($inputFileName);

function convert($size)
{
    $unit=array('b','kb','mb','gb','tb','pb');
    return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}

/**  Define a Read Filter class implementing PHPExcel_Reader_IReadFilter  */
class chunkReadFilter implements PHPExcel_Reader_IReadFilter
{
    private $_startRow = 0;

    private $_endRow = 0;

    /**  Set the list of rows that we want to read  */
    public function setRows($startRow, $chunkSize) {
        $this->_startRow    = $startRow;
        $this->_endRow        = $startRow + $chunkSize;
    }

    public function readCell($column, $row, $worksheetName = '') {
        //  Only read the heading row, and the rows that are configured in     $this->_startRow and $this->_endRow
        if (($row == 1) || ($row >= $this->_startRow && $row < $this->_endRow)){
            return true;
        }
    return false;
    }
}

/**  Create a new Reader of the type defined in $inputFileType  **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);



echo '<hr />';


/**  Define how many rows we want to read for each "chunk"  **/
$chunkSize = 25;
/**  Create a new Instance of our Read Filter  **/
$chunkFilter = new chunkReadFilter();

/**  Tell the Reader that we want to use the Read Filter that we've Instantiated  **/
$objReader->setReadFilter($chunkFilter);

/**  Loop to read our worksheet in "chunk size" blocks  **/
/**  $startRow is set to 2 initially because we always read the headings in row     #1  **/

for ($startRow = 2; $startRow <= 100; $startRow += $chunkSize) {

    /**  Tell the Read Filter, the limits on which rows we want to read this     iteration  **/
    $chunkFilter->setRows($startRow,$chunkSize);
    /**  Load only the rows that match our filter from $inputFileName to a PHPExcel Object  **/
    $objPHPExcel = $objReader->load($inputFileName);

    //    Do some processing here

    $sheetData = $objPHPExcel->getActiveSheet();
    $highestRow = $sheetData->getHighestRow();
    //$sheetData = $sheetData->toArray(null,true,true,true);
    //var_dump($sheetData);
    echo '<br /><br />';
    echo convert(memory_get_peak_usage(true));
}
?>

并且在运行时会输出此响应。

277 mb
294.5 mb
295.5 mb
296.75 mb

它一次读取 25 行,依此类推。我想不通的是,为什么内存峰值一直在上升?

我知道在处理之前必须读取整个 Excel 文件,但肯定每次都应该使用相同数量的内存,因此内存使用量不会随着时间的推移而发生很大变化。然而,它似乎在不断上升,我不知道是为什么。

【问题讨论】:

  • 一个单元格可能包含一个数字,可以很容易地表示为一个固定长度的浮点数或整数值,另一个单元格可能包含一个 200 个字符的字符串
  • 因为一个单元格可能有很多样式信息,另一个单元格只有默认样式信息
  • 因为当您读取新块时,前一个块读取的样式不会从内存中刷新,这将读取该块中单元格使用的其他样式

标签: php memory phpexcel


【解决方案1】:

在使用 PHPExcel 时,您可以采取许多措施来减少内存。我建议您在 Apache 中修改服务器的内存限制之前,采取以下措施来优化内存使用。

/* Use the setReadDataOnly(true);*/
    $objReader->setReadDataOnly(true);

/*Load only Specific Sheets*/
    $objReader->setLoadSheetsOnly( array("1", "6", "6-1", "6-2", "6-3", "6-4", "6-5", "6-6", "6-7", "6-8") );

/*Free memory when you are done with a file*/
$objPHPExcel->disconnectWorksheets();
   unset($objPHPExcel);

避免使用非常大的 Exel 文件,记住是文件大小导致进程运行缓慢和崩溃。

避免使用 getCalculatedValue();读取单元格时的函数。

【讨论】:

    【解决方案2】:

    即使您按块读取数据,PHPExcel 也会在内存中保存电子表格的表示形式。您读取的数据越多,您需要的内存就越多。

    将表示保存在内存中对于能够在电子表格中的任何位置添加/编辑单元格以及对行/列进行一些计算(例如,要调整列的宽度,您需要知道宽度该列中的每个非空单元格并将所有数据都保存在内存中可以更容易地检索)。

    通常,您读取的每个单元格将占用 1K 的内存。您可以通过使用 PHPExcel 提供的不同缓存机制来优化这一点。虽然内存优化会带来性能损失,但这是一种权衡。

    【讨论】:

    • 但是如果我读取第 1-200 行,它肯定应该使用与 201-400 相同的内存量,因为它是相同数量的数据?
    • 是的,据我所知
    • 那为什么内存会增加呢?
    • 如果你读 200 行,不管你读的是哪一行,它应该需要相同数量的内存。在这里,您有一个循环并读取 mulitple 块。第一个块始终占用 277MB(在您的示例中),但读取的数据将在读取下一个块时保留在内存中,从而增加内存使用量。
    • 读取数据到底是什么意思?我会认为一旦读取了某些内容,就应该将其从内存中丢弃。
    【解决方案3】:

    我遇到了类似的问题,我相信我已经找到了 PHPExcel 库的 PHPExcel_Calculation 类。在我的测试中,我看到它的 $_workbookSets 数组从未被清空,并且随着每次块迭代继续添加更多实例。

    不幸的是,我无法找到确切的原因,但似乎 unsetInstance() 方法仅在脚本执行的最后调用,即调用 PHPExcel 类析构函数时。

    调用 disconnectWorksheets() 方法对修复此问题没有任何效果,也没有通过 gc_collect_cycles() 强制 PHP 的垃圾收集。

    我的临时解决方案是向 Calculation 类添加一个新的 unsetInstances() 静态方法,它将 $_workbookSets 设置为一个空数组,然后在我的块循环结束时调用该方法。

    在 PHPExcel 库的 Calculation.php 中:

    public static function unsetInstances() {
      self::$_workbookSets = array();
    }
    

    然后在循环的最后一行调用该函数:

    PHPExcel_Calculation::unsetInstances();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-03
      • 2012-05-30
      • 2021-02-26
      • 2010-10-24
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多