15,020
edits
m (→避免的 coding 習慣) |
|||
| Line 112: | Line 112: | ||
=== 錯誤訊息: Allowed memory size of XXX bytes exhausted (out of memory) === | === 錯誤訊息: Allowed memory size of XXX bytes exhausted (out of memory) === | ||
* 訊息: PHP Fatal error: Allowed memory size of XXX bytes exhausted (tried to allocate XX bytes) | * 訊息: PHP Fatal error: Allowed memory size of XXX bytes exhausted (tried to allocate XX bytes) | ||
* 原因: | * 原因: 一次讀取13萬行的資料,發生錯誤等原因 | ||
* 解決方法: | * 解決方法: | ||
** 減少每次讀取的資料行數,例如每次只讀取 50 行的資料筆數來做處理。 | ** 增加 memory_limit<ref>[http://blog.xuite.net/chingwei/blog/30054402-%E3%80%90%E7%B3%BB%E7%B5%B1%E3%80%91PHP+-+Fatal+error%3A+Allowed+memory+size+of+xxx+bytes+exhausted 【系統】PHP - Fatal error: Allowed memory size of xxx bytes exhausted @ My Life :: 隨意窩 Xuite日誌] </ref> | ||
** 跟檔案處理有關: (1) 減少每次讀取的資料行數,例如每次只讀取 50 行的資料筆數來做處理。 (2) 避免使用一次讀取全部檔案內容的 [http://php.net/manual/en/function.file-get-contents.php file_get_contents]、[http://php.net/manual/en/function.file-put-contents.php file_put_contents]等函數 | |||
** [http://php.net/manual/en/control-structures.foreach.php foreach] 時,與其寫入整個陣列到記憶體,可以改用 Generators 寫法,節省記憶體的使用。詳見: [http://php.net/manual/en/language.generators.overview.php PHP: Generators overview - Manual] | ** [http://php.net/manual/en/control-structures.foreach.php foreach] 時,與其寫入整個陣列到記憶體,可以改用 Generators 寫法,節省記憶體的使用。詳見: [http://php.net/manual/en/language.generators.overview.php PHP: Generators overview - Manual] | ||