"Allowed memory size exhausted" in PHP — What It Means and How to Fix It
What the PHP error "Allowed memory size exhausted" means and how to fix it.
Published October 31, 2026
The script used more memory than memory_limit allows. Raising the limit may help temporarily, but the real fix is usually using less memory.
What it means and why it happens
Typical causes: loading huge result sets or files into arrays, infinite recursion or loops, or large images and exports.
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes)
Practical examples
Code that triggers it
<?php
$rows = $pdo->query('SELECT * FROM big_table')->fetchAll(); // everything in memoryThe fix
<?php
$stmt = $pdo->query('SELECT * FROM big_table');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { process($row); } // streamingRaise the limit carefully
; php.ini
memory_limit = 256M
// or at runtime
ini_set('memory_limit', '256M');Common mistakes
- Only raising memory_limit without fixing the cause.
- Loading full files with file_get_contents.
- Leaks in long-running scripts.
Key takeaways
- Allowed memory size exhausted: The script used more memory than memory_limit allows.
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
How do I process big files?
Read them line by line with fgets or generators.
How do I find which line caused Allowed memory size exhausted?
The error message and stack trace show the file and line; start there and work backwards.