首页 > 解决方案 > 运行 PHPExcel 的内存使用情况

问题描述

所以我经历过我的谷歌标签有一段时间变白了,在某些情况下,即使我几乎没有打开任何标签,我的一个 PHP 页面也没有响应,并且在使用一些调试之后,echo memory_get_usage() . "<br>";我发现我的PHPExcel函数是从2047192函数之前开始的在它之后44654216。我不知道这是否很多,是什么原因导致 google chrome 选项卡“崩溃”,或者还有其他工具可以对此进行更多分析?我确实尝试php_value memory_limit '49M'在我的.htaccess文件中实现,如果它低于这个值,那么使用的函数会PHPExcel写入:

Fatal error: Allowed memory size of 47185920 bytes exhausted (tried to allocate 4096 bytes) in File/Path...

此函数中增加此内存的行是:

line 4: $objPHPExcel = $objReader->load($path);

我不确定现在该怎么办,因为我以前从未经历过这种情况。使用的函数PHPExcel是下面的函数,所以也许你可以看到它的一些问题(如果有的话)。

function RVTM_Excel($fileName, $overviewResult, $path, $firstItemDD){
    $type = PHPExcel_IOFactory::identify($path);
    $objReader = PHPExcel_IOFactory::createReader($type);
    $objPHPExcel = $objReader->load($path);

    //  Get worksheet dimensions
    $countSheets = $objPHPExcel->getSheetCount();

    for ($i=0; $i < $countSheets; $i++) {
        $sheet = $objPHPExcel->setActiveSheetIndex($i);

        $row = $objPHPExcel->getActiveSheet($sheet)->getRowIterator(1)->current();

        $cellIterator = $row->getCellIterator();
        $cellIterator->setIterateOnlyExistingCells(false);

        foreach ($cellIterator as $cell) {
            //  Searching for column with Area of Relevance
            if ($cell->getValue() == "Area of Relevance") {
                $colIndex = PHPExcel_Cell::columnIndexFromString($cell->getColumn());
                $colString = PHPExcel_Cell::stringFromColumnIndex($colIndex-1, false);
                $letterRelevance = $colString;
            }
            //  Searching for column with SystemFeatures
            if ($cell->getValue() == "SystemFeatures") {
                $colIndex = PHPExcel_Cell::columnIndexFromString($cell->getColumn());
                $colString = PHPExcel_Cell::stringFromColumnIndex($colIndex-1, false);
                $letterFeatures = $colString;
            }
            //  Searching for column with Base_ID
            if ($cell->getValue() == "Base_ID" || $cell->getValue() == "ID") {
                $colIndex = PHPExcel_Cell::columnIndexFromString($cell->getColumn());
                $colString = PHPExcel_Cell::stringFromColumnIndex($colIndex-1, false);
                $letterBaseID = $colString;
            }
        }
        if (!empty($letterRelevance)) {
            $highestRow = $sheet->getHighestRow();
            for ($row = 0; $row <= $highestRow; $row++){
                //  Read a row of data into an array Column: A = "ID" and Column: D = "Area of Relevance"
                $rowDataA = $sheet->rangeToArray($letterBaseID . $row,
                                                        NULL,
                                                        TRUE,
                                                        FALSE);

                $rowDataD = $sheet->rangeToArray($letterRelevance . $row,
                NULL,
                TRUE,
                FALSE);

                $rowDataC = $sheet->rangeToArray($letterFeatures . $row,
                                                        NULL,
                                                        TRUE,
                                                        FALSE);

                if (strpos($rowDataD[0][0], 'HEADING') === false && strpos($rowDataD[0][0], 'SW') !== false && is_numeric($rowDataA[0][0])) {
                    $rowData1[] = $rowDataA[0][0];
                    $rowData2[] = $rowDataC[0][0];
                    $rowData3[] = $rowDataD[0][0];
                    $idArray =  array_combine($rowData1, $rowData2);
                    //If Area of relevance is needed make one with rowData1 combined with rowData3
                }
            }
        }
    }
    if (isset($idArray)){
        //Sizeof = 709
        $_SESSION['Dashboard_IDarray'] = $idArray;
    }
}

更多信息

现在因为我对此不太了解,所以我从 google chromes 浏览器进行了一些测试。我真的没有看到图表有任何问题,但是你们可能吗?

谷歌表现

谷歌表现

如果有任何帮助,还做了一个HEAP 快照

堆

如果有人能指出我正确的方向,这可能是什么,那就太好了。我确实尝试清除我的缓存(350MB),但没有改变任何东西。

标签: phpgoogle-chromememoryphpexcel

解决方案


推荐阅读