Fix "Allowed Memory Size Exhausted" in WordPress
Clear WordPress's "allowed memory size exhausted" fatal error with one wp-config.php line, then find out if your host caps PHP or a plugin is leaking.
Fatal error: Allowed memory size of 268435456 bytes exhausted means a PHP request reached its memory limit, and the first number is that limit in bytes. 268435456 bytes is 256M. The fix that works on most sites is one line in wp-config.php, placed above the /* That's all, stop editing! */ comment, with a value higher than the number in your error:
define( 'WP_MEMORY_LIMIT', '512M' );
For a 256M error that means 512M. For a 128M error (134217728 bytes), try 256M first. A value equal to or lower than the limit in the error changes nothing, because WordPress only ever raises PHP’s limit. If the site crashes again at the new number, the limit was never the real problem, and the second half of this page is about finding what is using the memory.
What the numbers in the error mean
PHP counts the memory each request allocates. When an allocation would take the request past memory_limit, PHP stops it with a fatal error. The message carries two numbers: the limit that was in force, and the size of the one allocation that did not fit (the “tried to allocate” part). The second number is only the last request for memory, so it tells you little about what used up the rest. Use this table to translate the first one:
| Bytes in the error | MB equivalent |
|---|---|
| 134217728 | 128M |
| 268435456 | 256M |
| 536870912 | 512M |
| 1073741824 | 1024M (1G) |
What our test showed
In that run we repeated the same 320M request with WP_MEMORY_LIMIT unset and then at four values, each time above the stop-editing line:
WP_MEMORY_LIMIT in wp-config.php | Limit PHP reported | 320M request |
|---|---|---|
| Not set (WordPress default, 40M) | 256M | Fatal error |
64M | 256M | Fatal error |
256M | 256M | Fatal error |
512M | 512M | Worked |
1024M | 1024M | Worked |
The 64M and 256M rows changed nothing because PHP already allowed 256M and WordPress does not lower a limit. Only a value above the number in the error helped. We also imitated a host that sets PHP to 128M, with an ini_set() at the top of wp-config.php. With no constant set, normal requests ran at 128M while WordPress still listed WP_MEMORY_LIMIT as 40M, admin screens ran at 256M, and a 180M request failed. With WP_MEMORY_LIMIT at 256M, the limit became 256M and the 180M request worked.
Playground lets PHP change memory_limit while a request runs. A host can block that, and then the same line does nothing; step 2 below covers that case.
How WordPress sets the limit
This part is read from the WordPress 7.1.4 source (default-constants.php, wp-admin/admin.php and wp_raise_memory_limit() in functions.php), not run, except where the test above confirms it.
| Constant | Default if you don’t set it | Where it applies |
|---|---|---|
WP_MEMORY_LIMIT | 40M on a single site, 64M on multisite. If PHP doesn’t allow the limit to change at runtime, whatever PHP is set to. | Every request. WordPress raises PHP’s limit to this value when it loads, and only if the value is higher than the current limit. |
WP_MAX_MEMORY_LIMIT | 256M. If PHP is already above 256M or unlimited, the PHP value. If you set WP_MEMORY_LIMIT above 256M, that value. | Admin screens for users who can manage options (administrators on a normal site), image processing for any user, and WP-Cron runs. |
That table explains a common puzzle: Site Health lists WP_MEMORY_LIMIT as 40M while the site really runs at 256M. The constant keeps its default, PHP keeps its higher limit, and WordPress never lowers it. Our test reported exactly that pair of values.
The higher admin limit comes from wp_raise_memory_limit( 'admin' ), which wp-admin/admin.php calls only when the current user can manage_options. An editor or author in the dashboard stays at the normal limit, except while WordPress resizes an image, which uses its own image context for every role. Each context has a filter of its own (admin_memory_limit, image_memory_limit and cron_memory_limit) if a plugin needs to change it.
Placement matters for a reason you can see in wp-settings.php. The last line of wp-config.php loads that file, and it defines both memory constants with their defaults right at the start. A define() placed after the require_once ABSPATH . 'wp-settings.php'; line therefore comes too late: PHP keeps the default and raises a warning that the constant is already defined.
Fix it, in order
-
Raise the WordPress limit in
wp-config.php. It is reversible and touches nothing else.define( 'WP_MEMORY_LIMIT', '512M' );Choose a value above the number in your error. You only need
WP_MAX_MEMORY_LIMITwhen the crash happens on admin screens, during image processing or in cron, and even then aWP_MEMORY_LIMITabove 256M carries over to it by default (512M on admin screens in our test). The wp-config.php generator has a memory field in MB that writes this line along with debug, revision and file editor settings; enter a number above the one in your error, such as 512 for a 256M error. Our guide to editing wp-config.php shows where the file lives and how to edit it without locking yourself out. -
If the limit doesn’t move, your host is fixing it. WordPress can only change
memory_limitwhen PHP allows a change at runtime. The PHP manual says a value set withphp_admin_valuecannot be overridden by.htaccessorini_set(). Raise it at the PHP level instead. Inphp.ini, or in a.user.inifile in your web root:memory_limit = 512MPHP reads
.user.inionly when it runs as CGI or FastCGI (PHP-FPM is FastCGI), and by default it re-reads the file every 300 seconds, so a change can take five minutes to show. On Apache with mod_php you can use.htaccess:<IfModule mod_php.c> php_value memory_limit 512M </IfModule>php_valueis a directive of mod_php, the module that runs PHP inside Apache. When PHP runs through PHP-FPM or another handler, Apache doesn’t know the directive, and Apache’s documentation says an unknown word in.htaccessproduces a server error (HTTP 500) when overrides are allowed. The<IfModule>wrapper skips the line when mod_php isn’t loaded. Apache accepts the module’s source file name there, which ismod_php.cfor PHP 8 andmod_php7.cfor PHP 7. Nginx never reads.htaccess, so on nginx the line does nothing at all. The same split applies toupload_max_filesizeandpost_max_size, and our guide to raising the upload limit covers that side. -
Confirm the value that is actually live. Don’t trust the file you edited; trust what WordPress reports. In Tools > Site Health > Info, open the Server section. “PHP memory limit” is the value for normal requests. When WordPress raised the limit for the dashboard, a second row, “PHP memory limit (only for admin screens)”, shows that value. The WordPress Constants section lists
WP_MEMORY_LIMITandWP_MAX_MEMORY_LIMIT. (Those labels are read from the WordPress 7.1.4 Site Health code.)If you can’t reach the dashboard, a one-line file at the web root shows the value PHP starts with before WordPress loads:
<?php echo ini_get('memory_limit');Load it in a browser, read the value, then delete the file. If it shows 128M and Site Health still shows 128M after your change, the host is overriding you, and the fix is their control panel setting or a support ticket.
The part most guides skip
Raising the limit is a test. Here is how to read the result.
If you set 512M and the site stays up under normal traffic, the ceiling was simply too low for what the site does. Large media libraries, WooCommerce and page builders are common reasons. You are done.
If the error comes back at the new number, stop raising it. When the crash follows the limit upward, something is looping or piling up data: a plugin loading a whole database table into an array, an add_action callback that triggers itself, an import routine holding every row in memory, or two plugins fighting inside the same hook. A higher limit doesn’t end an unbounded loop. It moves the crash a little later and makes it harder to reproduce.
To find the culprit, deactivate all plugins and switch to a default theme like Twenty Twenty-Five. If the error stops, reactivate one at a time until it returns. Turn on the debug log to catch where it dies:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
The fatal line in wp-content/debug.log names the file and line that made the last allocation, which is usually the loop or one call away from it. Paste that line into the PHP fatal error decoder to get the limit in MB and the plugin or theme folder the path points into. If debug.log doesn’t appear, the debug log location guide explains where WordPress writes it and why it can be missing. Set WP_DEBUG back to false once you have the line; the WordPress debug mode guide covers turning it off cleanly.
If the crash happens while uploading images, the memory goes into decoding and resizing the picture, which our HTTP error on image upload guide measures for common camera and phone sizes.
When the request dies with nothing in debug.log
Recent PHP releases changed what happens at the limit. The PHP 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 usually allowed to finish, and the familiar fatal error follows. A separate hard limit, by default twice memory_limit when that is 1G or less and memory_limit plus 1G above that, ends the process straight away with exit status 137.
That hard stop leaves no trace where WordPress users look. The manual says its message, “Hard memory limit of N bytes exhausted”, is written to the standard error stream and not to error_log, so it never reaches debug.log, and shutdown functions don’t run. If a request ends with no response from PHP and an empty debug.log, ask your host for the PHP process’s error output. Exit status 137 is also what a process killed by the operating system’s out-of-memory killer reports, so that output is the way to tell the two apart.
What not to do
Don’t set WP_MEMORY_LIMIT to -1 or 1024M and forget it. Unlimited memory does not fix a leak. It lets one runaway request take as much memory as the server will give it, which on shared hardware can slow every other site on the box. On the PHP versions above, -1 also removes the hard limit. A ceiling is a safety feature, so keep one.
Don’t paste ini_set('memory_limit', '512M') into functions.php as the main fix. It runs too late for a crash that happens while plugins load, before the theme’s functions.php runs, and it only runs on requests that load that theme. wp-config.php is read earlier and is the right place.
Don’t assume it is a hosting problem and upgrade your plan. Sometimes the hosting really is too small. Often the same crash follows you to the bigger plan because the cause came along in your plugins. Rule out the loop first; it costs nothing.
Don’t raise memory_limit to fix a max_execution_time error. They are different limits with different messages. Memory exhaustion says “Allowed memory size … exhausted.” A timeout says “Maximum execution time … exceeded.”
Still stuck?
If Site Health confirms the live limit is 512M or higher, you have tested with all plugins off and a default theme, and memory still runs out, you have a real leak to chase in the debug log rather than a ceiling to raise. Capture the fatal line and note which plugin’s folder it points into. That is your bug report for the plugin’s author.
Sources
- WordPress Developer Resources: wp-config.php (WP_MEMORY_LIMIT, WP_MAX_MEMORY_LIMIT), wp_raise_memory_limit()
- WordPress 7.1.4 source code: default-constants.php, wp-admin/admin.php, functions.php, wp-settings.php, Site Health info, class-wp-debug-data.php
- PHP manual: memory_limit, memory_hard_limit_percent, .user.ini files, How to change configuration settings
- PHP source code: mod_php.c, the Apache module in PHP 8.3
- Apache HTTP Server documentation: .htaccess files tutorial, IfModule directive
William Lopez is the founder of Odie Themes. He builds the free WordPress tools and themes on this site and has been on WordPress.org since December 2019.
FAQ
Questions
How do I fix "Allowed memory size exhausted" in WordPress?
Read the first number in the error: it is the limit PHP was running with, in bytes. Add define( 'WP_MEMORY_LIMIT', '512M' ); to wp-config.php above the /* That's all, stop editing! */ line, using a value higher than that number (512M for a 268435456-byte error). WordPress only ever raises PHP's limit, so a value at or below the number in the error changes nothing.
How many MB is 268435456 bytes in the PHP memory error?
268435456 bytes is 256M. For reference, 134217728 is 128M and 536870912 is 512M. That first number is the memory limit that was in force. The number after "tried to allocate" is the size of the one allocation that no longer fit, which says little about what used up the rest.
Why doesn't editing wp-config.php change my PHP memory limit?
Either the value is not higher than the current limit, because WordPress never lowers it, or your host does not let PHP change memory_limit at runtime. The PHP manual says a value set with php_admin_value cannot be overridden by .htaccess or ini_set(). Check Tools > Site Health > Info > Server for the live value, then raise it in php.ini or .user.ini, or ask the host.
Why does php_value memory_limit in .htaccess cause a 500 error?
php_value belongs to mod_php, the Apache module that runs PHP inside Apache. When PHP runs through PHP-FPM or another handler, Apache doesn't recognize the directive, and Apache's documentation says an unknown directive in .htaccess produces a server error (HTTP 500). Wrap the line in <IfModule mod_php.c> or use a .user.ini file instead. Nginx never reads .htaccess, so there the line does nothing.
Where can I see the memory limit WordPress is really using?
In Tools > Site Health > Info, open the Server section. "PHP memory limit" is the value for normal requests, and when WordPress raised it for the dashboard a second row, "PHP memory limit (only for admin screens)", shows that value. The WordPress Constants section lists WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT.
Is it safe to set the WordPress memory limit to unlimited?
No. Unlimited memory does not fix a leak. It lets one runaway request take as much memory as the server will give it, and on PHP versions with the new hard limit, -1 switches that safety net off as well. Keep a ceiling, and if 512M still crashes, look for the loop instead of raising it again.