Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

PHP "Allowed memory size of ... bytes exhausted": raise memory_limit only after finding the cause

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

PHP raises this fatal error when one request tries to allocate more memory than its memory_limit allows. In the PHP source the message reads "Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)", so a real line shows two numbers: the limit that applied and the size of the allocation that failed. Raising the limit stops the error, but it only helps if the work is legitimate. Find the limit that actually applied, find what used the memory, and then choose a new value you can justify.

What the error means

The PHP manual describes memory_limit as the setting that "sets the maximum amount of memory in bytes that a script is allowed to allocate". Its default is "128M" and it is changeable in every mode (INI_ALL), and "to have no memory limit, set this directive to -1".

The first number in the message is that limit in bytes. Shorthand values multiply out: 128M is 128 × 1,048,576 = 134,217,728 bytes, the number you see on a default install. 256M is 268,435,456 bytes. If the number in your error is not one you set, another setting is overriding it, which the next section covers.

Newer PHP releases add a second boundary. The manual says that as of PHP 8.5.12, 8.4.27, 8.3.36 and 8.2.35, memory_limit "is a soft limit": an allocation that crosses it "is, in most cases, allowed to complete before the fatal error is raised". A separate hard limit, memory_hard_limit_percent, bounds how far it can be exceeded. When an allocation would exceed the hard limit, "the process is terminated immediately with exit status 137" and a message such as "Hard memory limit of 201326592 bytes exhausted" is written to the standard error stream. That message "does not go through the error handling mechanism", so it is not in error_log. With the default settings the manual gives the hard limit as twice memory_limit when memory_limit is 1G or less, so 128M gives 256M.

Likely causes

Cause What you see How to confirm
Legitimate work needs more than the limit The error appears on one job, such as an import, export, image resize or report The line names a file and the request is large or repeatable
Code loads too much at once Memory use grows with data size; the same request worked when the data was smaller The failing allocation is large, or the file path points at one plugin or script
A different limit applies than you think The number in the error is not the one in your php.ini The pool, a .user.ini or another php.ini sets memory_limit
post_max_size is large for the limit The PHP manual says "Generally speaking, memory_limit should be larger than post_max_size" post_max_size is close to or above memory_limit
A hard limit or the kernel ended the process No PHP error line in error_log; with PHP-FPM, "the client receives no response from PHP"; exit status 137 Standard error shows the hard-limit message, or the kernel log shows an out-of-memory kill

Prove the cause before changing anything

1. Read the log line. It names the limit in bytes, the allocation size, and the file and line where PHP stopped. Convert the first number to megabytes: divide by 1,048,576.

2. Find the value that applies to the web path, not the command line. php --ini shows "Configuration File (php.ini) Path" and "Loaded Configuration File" for the command-line program. The web path is a separate program (FPM) and may load a different file or a pool override, so do not trust the command-line answer for it.

Risk: a temporary script in the document root is reachable by anyone who knows its name, and it discloses a server path, so you will request it once, then remove only that file.

To read what FPM applies, put a tiny temporary script in the site's document root, with a long random name of your own (for example memcheck-k8d2x7q9.php):

<?php echo ini_get('memory_limit'), "\n", php_ini_loaded_file(), "\n";

Request it through the web server. ini_get reports the current value for that request, so it includes any pool override, and php_ini_loaded_file() reports the path of the php.ini that was loaded.

After you have requested it, show the exact file first and remove only that one, using your own random name:

ls -l /var/www/app/public/memcheck-k8d2x7q9.php
rm /var/www/app/public/memcheck-k8d2x7q9.php

To back out of this step, there is nothing to restore; the file is only a test.

3. Read the FPM pool configuration. The PHP manual shows php_admin_value[memory_limit] = 32M as an example pool setting and adds that "Settings defined with php_admin_value and php_admin_flag cannot be overridden with ini_set()". So a pool value beats a script's own ini_set. Read each pool file for memory_limit.

4. Check the web server. The manual shows an nginx example that passes PHP settings as FastCGI parameters (fastcgi_param PHP_VALUE) and warns that these are passed as headers. If your configuration uses it, a value there can also apply.

5. For WordPress, read wp-config.php. The Editing wp-config.php page says WP_MEMORY_LIMIT "allows you to specify the maximum amount of memory that can be consumed by PHP", that by default WordPress "will attempt to increase memory allocated to PHP to 40MB" for a single site (64MB for multisite), and that it "increases PHP Memory only for WordPress, not other applications". WP_MAX_MEMORY_LIMIT is used in the administration area.

6. Rule out the hard limit and the kernel. If requests end with no PHP error line, check the process manager's log and the kernel log. The manual notes that exit status 137 "is also the status reported for a process killed by SIGKILL, for example by the kernel OOM killer or a container runtime, so check the standard error stream to tell the cases apart." For the kernel case see "Linux OOM Killer on a VM: Find the Cause, Set Memory Limits".

Fix

Raise the limit by a measured amount

Choose the new value from what the work needs. If the failing job loaded a 90 MB file and PHP needs about twice that to process it, 256M (268,435,456 bytes) is an informed choice; doubling blindly is not.

Then check what the change costs in the worst case. The limit applies to each request, and FPM runs several at once: the manual says pm.max_children "sets the limit on the number of simultaneous requests that will be served". With pm.max_children at 8, a 256M limit allows up to 8 × 256M = 2,048M (2,048 × 1,048,576 = 2,147,483,648 bytes, or 2 GiB) in use if every worker hits its limit together. Real use is lower, but that is the ceiling to compare with the server's RAM. For sizing workers see "PHP-FPM Has Reached pm.max_children: Measure Queueing Before Adding Workers".

Set it where it applies:

  • If a pool sets it, change php_admin_value[memory_limit] in that pool file.
  • Otherwise change memory_limit in the php.ini the web path loads, which step 2 reported.

Save the file, test the configuration, then reload. If the value lives in a pool file, save that pool file the same way, with a dated copy, and use its name in the back-out. As an illustration, for a php.ini at /etc/php/8.4/fpm/php.ini:

cp -p /etc/php/8.4/fpm/php.ini /etc/php/8.4/fpm/php.ini.2026-11-20
php-fpm -t

Disruptive step: reloading the FPM service with the systemctl reload command below asks it to read its configuration again. The PHP manual does not describe what happens to requests in flight during a reload, so reload in a quiet period.

The php-fpm help text in the PHP source reads "-t, --test Test FPM configuration and exit". Use the php-fpm binary your distribution installs, which may carry a version in its name. Edit the value, run the test again, then reload:

systemctl reload php8.4-fpm

systemctl reload "will reload the service-specific configuration" of the named unit, and the unit name depends on your system.

To back out, restore the copy you saved under the name you saved it, test, and reload again:

cp -p /etc/php/8.4/fpm/php.ini.2026-11-20 /etc/php/8.4/fpm/php.ini
php-fpm -t
systemctl reload php8.4-fpm

Do not set memory_limit to -1 as a cure

The manual allows -1 for "no memory limit". The manual says the limit "helps prevent poorly written scripts for eating up all available memory on a server", and with -1 that protection is gone. It also says that when memory_limit is -1 there is no hard limit either. Choose a measured value instead.

Fix the code or the job

If memory use grows with data size, process in smaller pieces, or stream a file instead of loading it whole. If a plugin is at fault, update it or replace it; the file path in the log names it.

Check post_max_size

The manual says "Generally speaking, memory_limit should be larger than post_max_size." If you allow large uploads, raise both thoughtfully, and see "The uploaded file exceeds the upload_max_filesize directive in php.ini": every limit on the path.

Example (illustrative): On web01, a WordPress site's product import fails. The log says "Allowed memory size of 134217728 bytes exhausted (tried to allocate 20971520 bytes)". 134,217,728 ÷ 1,048,576 = 128, so the limit is 128M, the default; 20,971,520 ÷ 1,048,576 = 20, so the failed allocation was 20M. The memcheck script reports 128M and /etc/php/8.4/fpm/php.ini, and no pool overrides it. The import loads the whole CSV at once. The administrator raises memory_limit to 256M after checking 8 × 256M = 2,048M (2 GiB) against the server's RAM, and asks the plugin's author about streaming the file.

Confirm the fix

  1. The memcheck request (while the test file still exists) reports the new value, then you remove the file.
  2. The failing job completes, and the same log line does not return.
  3. Memory use of the FPM workers during the job stays below the figure you computed for the server.
  4. If the limit did not change, you edited a file that is not the one in use. Return to step 2 and step 3 of the checks.

What to watch afterwards

  • Alert on the PHP error log for this message, not on visitor reports.
  • Record the largest memory use of the job after the change, so the next increase is based on a measurement.
  • Recalculate the worst case (workers × limit) whenever you change pm.max_children or memory_limit.
  • After a PHP upgrade, check the effective value again: a package update can replace a php.ini you edited.

Risks and limits

  • These commands have not been run or lab-tested.
  • The message wording is from the PHP-8.4 branch source. Debug builds add the file and line to the message.
  • The soft and hard limit behaviour applies to the releases the manual names and later. On older releases the limit is not described as a soft limit.
  • The manual warns about the hard-limit setting: "If this directive is not recognized, the PHP version in use is affected by these vulnerabilities and must be updated immediately." It refers to the security releases of 21 October 2026.
  • systemctl unit names, php.ini paths and the php-fpm binary name differ between distributions. Use the paths your own checks reported.

Sources