October 29, 2026 - 10:50
PHP Allowed Memory Size Exhausted: Diagnose Before Raising the Limit Image
PHP

PHP Allowed Memory Size Exhausted: Diagnose Before Raising the Limit

0 Comments

Allowed memory size exhausted means a PHP request exceeded its configured memory limit. It does not, by itself, prove that the server has run out of RAM. A web request, a command-line job and a queue worker may use different PHP configurations, so first identify the process, input size and runtime that hit the limit.

Measure the limit and the peak in context

In a controlled test, log ini_get('memory_limit') and measure memory_get_peak_usage(true) near the end of the operation. With true, PHP reports memory allocated from the system; without it, the function reports memory currently used by PHP's allocator. Add measurements at a few meaningful stages if you need to find where the increase begins.

GENEL
error_log('memory_limit=' . ini_get('memory_limit'));
error_log('peak_bytes=' . memory_get_peak_usage(true));

Keep diagnostic values in server logs, not on a public page. Label each measurement with the processing step so repeated records can be compared. PHP's peak-memory reference explains the function's return value.

Look for data that grows or gets copied

Loading every database row into memory, decoding a large image, or building another full copy of an array can push a request over its limit. When the job permits it, process rows in batches, stream large files instead of reading them all at once, and release references that are no longer needed. Check whether a loop is accumulating results the caller never uses.

Offset-based pagination can become expensive for a large table; where a suitable index exists, advancing by an increasing key or cursor may be a better fit. Also consider concurrency: one request can sit below its limit while many simultaneous requests still put pressure on host RAM.

Raise the limit only after measurement

Increasing memory_limit can be reasonable for a measured workload, but it does not fix an accidental infinite loop, an inefficient query or unbounded input. Setting it to unlimited can expose other processes to resource exhaustion. Do not assume the CLI value matches PHP-FPM or Apache; the configuration file and SAPI may differ. For operating-system level investigation, keep PHP's per-request measurements separate from the Linux checks in high RAM usage and Linux process diagnosis.

Use PHP's core configuration directives as the reference for the setting, then verify the value from the same SAPI that runs the failing request.

Related Articles

Comments (0)

No comments yet. Be the first to comment!

Leave a Comment