2017-04-10 56 views
1

功能是像以下:PHPExcel库在while循环中导致内存增加?

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库,并告诉我,我怎么能初始化每一个之间的缓存,同时循环finaly回采使用内存的增加。

+0

什么while循环? –

+0

您是否将此函数的结果(它的返回值)添加到数组或类似的东西?如果你多次这样做,你会用完内存。也请向我们展示你的while循环。 –

+0

while循环只是一些其他与phpexcel无关的代码。就像 : – digjack

回答

2

试过吗?

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

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

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

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