簡體   English   中英

PHPExcel內存使用情況

[英]PHPExcel Memory Usage

我有以下代碼

<?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文件,然后才能對其進行處理,但可以肯定的是,每次都應使用相同的內存量,因此內存使用量不會隨時間變化很大。 但是,它似乎一直在上升,我不知道為什么。

使用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(); 讀取細胞時的功能。

即使您正在按塊讀取數據,PHPExcel也會保留電子表格的內存中表示形式。 您讀取的數據越多,所需的內存就越多。

將表示形式保存在內存中對於能夠在電子表格中的任何位置添加/編輯單元格以及對行/列進行一些計算非常有用(例如,要調整列的寬度,您需要知道每個單元格的寬度該列中的非空單元格,並將所有數據存儲在內存中使檢索起來更容易)。

通常,您讀取的每個單元格都會占用1K的內存。 您可以使用PHPExcel提供的不同緩存機制來對此進行優化。 盡管內存優化會帶來性能損失,但這是一個折衷。

我遇到了類似的問題,我相信我已經將其追溯到PHPExcel庫的PHPExcel_Calculation類。 在測試中,我看到它的$ _workbookSets數組從未被清空,並且每次塊迭代都繼續向其添加更多實例。

不幸的是,我無法找到確切的原因,但是似乎只有在腳本執行的最后,即調用PHPExcel類析構函數時才調用unsetInstance()方法。

調用disconnectWorksheets()方法對解決此問題沒有任何效果,也沒有通過gc_collect_cycles()強制PHP進行垃圾回收。

我的臨時解決方案是向Calculation類添加一個新的unsetInstances()靜態方法,該方法將$ _workbookSets設置為一個空數組,然后在我的塊循環結束時調用該方法。

在PHPExcel庫的Calculation.php中:

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

然后將函數作為循環的最后一行調用:

PHPExcel_Calculation::unsetInstances();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM