Lab-tested: PHP 8.4.27, memory_limit 16M · 2 Dec 2026 · Exhaustion reproduced, chunked fix verified in our lab
Fix Box — Try This First (10 Seconds)
Allowed memory size exhausted means one script outgrew its memory ceiling. The log line gives you the limit in bytes; divide by 1,048,576 to read it in MB.
- Read the log line: it names the limit, the failed allocation, and the file.
- Check the limit that actually applies to the web runtime, not just the CLI.
- Raise the limit for that job, or make the script process data in chunks. Still 500? Follow the 500 Decision Tree.
The Exact Symptoms
The browser shows 500 Internal Server Error, often during an import, backup, or report. The log carries the real message:
PHP Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 8388608 bytes) in /data/lab500b/www/memory.php on line 4
The first number is your limit: 16,777,216 bytes is 16 MB. The second number is the single allocation that crossed the line.
Diagnosis, Step by Step
First, confirm the limit for the runtime that failed. Web PHP and CLI PHP often read different configuration files.
php -r 'echo ini_get("memory_limit"), "\n";'
Next, find the script named in the log line. That is where the limit was hit, which is not always where the memory was wasted.
Then decide which kind of problem you have. A legitimate big job needs a higher limit; a script that loads everything at once needs smaller bites.
What Our Lab Reproduced
Our import-style script loaded 5,000,000 rows into one array under a 16 MB limit. The response was HTTP/1.0 500 Internal Server Error with the log line above.
The chunked version processed the same workload 1,000 rows at a time and freed each chunk. Under the same 16 MB limit it returned 200 OK with a peak of just 2,097,152 bytes (2 MB).
For comparison, the naive version needed a 256 MB limit to finish 200,000 rows. Its measured peak was 262,148,096 bytes, about 250 MB.
One honest surprise: our first test, 200,000 rows under 16 MB, returned 200. Since PHP 8.4.27, memory_limit is a soft limit, so a crossing allocation can complete before the fatal is raised.
The Fix
Raise the limit where the failing runtime reads it. For a single script, set it at the top:
ini_set('memory_limit', '256M');
For the whole site, set it in php.ini and restart PHP-FPM or Apache:
memory_limit = 256M
In WordPress, the same ceiling is set in wp-config.php:
define( 'WP_MEMORY_LIMIT', '256M' );
The durable fix is to use less memory. Process records in chunks and release each chunk before loading the next:
$total = 0;
for ($round = 0; $round < 200; $round++) {
$chunk = [];
for ($i = 0; $i < 1000; $i++) { $chunk[] = str_repeat('x', 1024); }
$total += count($chunk);
unset($chunk);
}
echo 'processed ' . $total . ' peak ' . memory_get_peak_usage(true);
That exact pattern is what our lab ran. It finished under a 16 MB limit with a 2 MB peak.
What Did Not Work
Our first reproduction attempt failed to trigger the error at all. With 200,000 rows, the soft limit in PHP 8.4.27 let the script finish, so the 500 never appeared.
Raising the limit alone also proved fragile. It moved the failure to a bigger dataset instead of removing it.
Prevention
Measure before you raise. Log memory_get_peak_usage(true) for big jobs, then size the limit from real numbers.
Avoid memory_limit = -1 on web requests. The limit is what stops one runaway script from taking down the whole server.
If the memory error arrives with a blank page, pair this guide with White Screen 500 Error. For other PHP fatals, see PHP 500 Error. Paste any log line into the Error Log Reader for a plain-language reading.
FAQ
How do I read the byte numbers in the message? Divide by 1,048,576. 16,777,216 bytes is 16 MB; 134,217,728 bytes is the common 128 MB default.
Is raising the limit enough? For a legitimate one-off job, often yes. For a script that grows with your data, chunking is the real fix.
Why did the error appear only during imports? Imports load many records at once, so they cross the ceiling first. Normal pages stay far below it.
Does the CLI limit match the web limit? Not necessarily. CLI, PHP-FPM, and Apache can each read a different php.ini.
What is a sensible ceiling for a shared site? 256 MB is the usual first step. If 512 MB is still not enough, suspect the code, not the limit.
Still not sure which layer failed? Start from the pillar guide, 500 Internal Server Error: Causes and How to Fix It, and follow its decision tree.
Sources and date
Lab tests run 2 Dec 2026 on PHP 8.4.27 with memory_limit 16M and 256M. References checked live on the same date.
- RFC 9110 §15.6.1 — 500 Internal Server Error: https://www.rfc-editor.org/rfc/rfc9110.html
- MDN — 500 Internal Server Error: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/500
- PHP Manual — Description of core php.ini directives (memory_limit): https://www.php.net/manual/en/ini.core.php






Leave a Reply