"Allowed Memory Size of 134217728 Bytes Exhausted" — The Emergency Brake of Your PHP Application
\n\nHave you ever seen this message in the middle of the night, when your site suddenly crashes, and the logs show white text on a black background? If so, you are not alone. Thousands of developers on Stack Overflow have wondered why their seemingly harmless PHP script suddenly "eats" all 128 megabytes of RAM and crashes with a fatal error.
\n\nLet's figure out what this error actually means, why it occurs, and most importantly, how to fix it once and for all. We won't just increase the memory limit — we'll learn to write code that doesn't gobble up resources like a hungry student in a cafeteria.
\n\nWhat Does "134217728 Bytes Exhausted" Mean?
\n\nThe number 134,217,728 is 128 megabytes. That's how much memory PHP is allowed to use for your script by default. When the script tries to allocate more — boom! — and you get a Fatal Error. This is a protective mechanism that prevents one script from killing the entire server.
But here's the catch: 128 MB is very little for modern applications. Processing a large XML file, generating a report from a database, working with images or heavy libraries — all of this can easily exceed the limit.
\n\nWhy Does This Happen? Three Main Reasons
\n\n- \n
- Greedy loops and recursion — when you load all data into memory at once, instead of processing it in chunks. \n
- Memory leaks — static variables, global arrays, and improper use of
staticcan accumulate data. \n - Heavy libraries and extensions — for example, image processing via GD or working with PDF. \n
How to Fix the Error: From Simple to Complex
\n\nMethod 1: Increase the Memory Limit (Quick Fix)
\n\nThe most obvious path is to simply give PHP more memory. But remember: this is not a cure, it's a painkiller. If your code is poorly written, it will eat up 512 MB and 1 GB as well.
\n\nThere are three ways to increase the limit:
\n\n1. In the php.ini file (permanent change):
\nmemory_limit = 256M\n\n2. In .htaccess (if available):
\nphp_value memory_limit 256M\n\n3. In the script itself (only for the current execution):
\nini_set('memory_limit', '256M');\n\nBut be warned: if you are on shared hosting, the host may not allow you to increase the limit above a certain threshold. And even if it does — don't abuse it.
\n\nMethod 2: Code Optimization — The Real Solution
\n\nInstead of giving the script more memory, let's teach it to use what it has wisely.
\n\nUse unset() to Free Memory
\n\nPHP is not C++, but manual memory management still works. When a large variable is no longer needed — delete it:
\n\n$data = file_get_contents('huge_file.csv');\n// process the data\nprocessData($data);\n// free the memory\nunset($data);\n\n\nProcess Data in Chunks
\n\nInstead of loading 100,000 rows from the database into memory, use cursors or limits:
\n\n// BAD: load everything into memory\n$users = $db->query("SELECT * FROM users")->fetchAll();\n\n// GOOD: process one row at a time\n$stmt = $db->query("SELECT * FROM users");\nwhile ($row = $stmt->fetch()) {\n processUser($row);\n // PHP will free memory after each iteration\n}\n\n\nUse Generators (yield)
\n\nGenerators are magic for working with large datasets. They don't store all values in memory:
\n\nfunction getLargeData() {\n for ($i = 0; $i < 1000000; $i++) {\n yield $i;\n }\n}\n\nforeach (getLargeData() as $value) {\n // process one element at a time\n echo $value . "\\n";\n}\