PHPExcel库在while循环中,导致内存增加?

时间:2017-04-10 09:00:12

标签: php

功能如下:

public function   getFile($fileFullPath){
               echo round(memory_get_usage()/1024/1024,2).'MB'.PHP_EOL;
        $PHPReader = PHPExcel_IOFactory::createReaderForFile($fileFullPath);
        if(! $PHPReader){
            return false;
        }
        $PHPExcel = $PHPReader->load($fileFullPath);
        $currentSheet = $PHPExcel->getActiveSheet();
                 return $currentSheet
}

该函数由while循环调用。并且在运行程序时,内存不断增加。输出信息如下:

➜  crontab git:(f_*****) ✗ php worker.php program_name.php

1.64MB
8MB
10.56MB
12.99MB
15.68MB
18.11MB
20.54MB
23.47MB
25.91MB
28.34MB
30.77MB

是熟悉PHPExcel库的人,告诉我如何在每个while循环之间初始化缓存,最终停止使用内存的增加。

2 个答案:

答案 0 :(得分:2)

试过这个?

$objPHPExcel->disconnectWorksheets(); 
unset($objPHPExcel); 

答案 1 :(得分:0)

感谢您的关注。我找到了解决方案。在每一个循环中。我删除了表格的缓存,如:

while(true){
   $currentSheet = $this->getFile($fullPath);
   ...  //do anything with $currentSheet;
   $currentSheet->disconnectWorksheets();   // drop the cache;
}

通过PHPlibrary的disconnectWorksheets。它删除了单元格的缓存。所以记忆停止增加。