Editing
Troubleshooting of PHP errors
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== 錯誤訊息: 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) <ref>[https://www.airpair.com/php/fatal-error-allowed-memory-size Fixing PHP Fatal Error: Allowed Memory Size Exhausted]</ref> * 可能原因: ** 一次讀取13萬行的資料,發生記憶體錯誤、 ** 無窮迴圈 (infinite loop) * 解決方法: ** 如果使用 32 位元 (x86) 的 PHP,改成使用 64 位元 (x64) 的 PHP ** 增加 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] 等函數,讀取檔案建議改成使用 [https://www.php.net/manual/en/language.generators.overview.php Generators] 或 [http://php.net/manual/en/function.fgets.php fgets]、而寫入檔案則可使用 [https://www.php.net/manual/en/function.fwrite.php fwrite] append 方式寫入<ref>[https://davidwalsh.name/basic-php-file-handling-create-open-read-write-append-close-delete Basic PHP File Handling -- Create, Open, Read, Write, Append, Close, and Delete]</ref>。 *** (3) 如果透過 MySQL 執行匯出資料表的 CSV 檔案,則可以不要選擇欄位名稱,而是選擇全部欄位直接匯出。原因:「Rather than attempting to build the object-tree, you could directly try to select the result into a file」<ref>[https://stackoverflow.com/questions/31471186/how-to-export-millions-of-rows-from-mysql-to-csv-via-php-without-exhausting-memo How to export millions of rows from MySQL to CSV via PHP without exhausting memory? - Stack Overflow]</ref> *** (4) 如果跟 json 檔案處理有關,可以搭配使用 [https://stedolan.github.io/jq/ jq] 可以有效處理大檔案的 json 檔案 ** [http://php.net/manual/en/control-structures.foreach.php foreach] 時,與其寫入整個陣列到記憶體,可以改用 [http://php.net/manual/en/language.generators.overview.php Generators] 寫法,節省記憶體的使用。 ** [https://errerrors.blogspot.com/2021/03/allowed-memory-exhausted.html 解決網站伺服器遇到 Allowed memory exhausted 問題]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information