PHPExcel記憶體流失問題

來源:互聯網
上載者:User

標籤:des   blog   http   io   使用   ar   for   資料   sp   

使用 PHPExcel 來產生 excel 文檔是比較消耗記憶體的,有時候可能會需要通過一個迴圈來把大資料切分成若干個小的 excel 文檔儲存來避免記憶體耗盡。

然而 PHPExcel 存在 circular references 的情況(貌似在最新的 1.6.5 版本中仍然沒有去解決這個問題),如果在一次 http 請求過程中反覆多次構建 PHPExcel 及 PHPExcel_Writer_Excel5 對象執行個體來完成多個 excel 文檔產生操作的話,所有被構建的對象執行個體都無法在 http 請求結束之前及時釋放,從而造成記憶體流失。

解決辦法是在 PHPExcel_Worksheet 類中增加方法:

 public function Destroy() {
     foreach($this->_cellCollection as $index => $dummy) {
         $this->_cellCollection[$index] = null;
     }
 }

並在 PHPExcel 類中增加方法:

 public function Destroy() {
     foreach($this->_workSheetCollection as $index => $dummy) {
         $this->_workSheetCollection[$index]->Destroy();
         $this->_workSheetCollection[$index] = null;
     }
 }

然後在需要資源回收的地方顯式的調用 PHPExcel::Destroy() 來處理循環參考的問題。注意 __destruct() 方法是在對象被認為可以被釋放的時候才會被調用,所以循環參考的處理不能放到 __destruct() 來進行。

PHPExcel記憶體流失問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.