Guide Errors & Troubleshooting

How to fix "allowed memory size exhausted"

PHP hit its memory ceiling and stopped rather than risk taking the whole server down with it — here is what to do about it.

Updated 7 min read Intermediate

The exact message is usually some version of: "Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes)". It means a PHP script tried to use more memory than it is allowed to, PHP stopped it rather than let it consume unlimited resources, and the numbers in the message are the ceiling and the amount that finally went over it.

This is a limit doing its job, not a sign of damage. Nothing about it implies lost data — the script simply stopped partway through, and whatever it hadn't yet saved or completed didn't happen, which on a page load usually just means the page failed to render.

Reading the numbers in the message

The first number is the current limit, in bytes. Divide by 1,048,576 to get megabytes — 134217728 bytes is 128MB, a common shared-hosting default. The second number is how much more the script was asking for at the moment it was stopped, which is rarely the whole story: a script can climb steadily through many smaller allocations before the one that finally tips it over the edge.

Raising the limit

This is worth doing first, both because it may be all that's needed and because it clears space to see whether the underlying task now completes normally.

In a plain PHP script, add near the top:

ini_set('memory_limit', '256M');

In WordPress, add to wp-config.php, above the line reading "That's all, stop editing":

define( 'WP_MEMORY_LIMIT', '256M' );

Many hosting accounts also expose a memory limit setting directly in the PHP configuration section of the control panel, which takes effect without editing any files and is worth checking first — a limit set there can otherwise override what you set in code.

Treat this as a diagnostic, not the end of the job

Doubling the limit and having the error disappear tells you the task needed more headroom — it does not tell you whether that need is legitimate or the sign of something using memory it shouldn't. The causes below are about telling the two apart.

Finding what is actually using the memory

1. A specific plugin or add-on

If the error only occurs on certain pages or certain admin screens, that narrows it immediately. Deactivating plugins one at a time and retesting the same page will identify a specific plugin far faster than reading code. Image-heavy plugins, page builders, and anything that processes large datasets in one pass are the usual suspects.

2. A large import, export, or bulk operation

Operations that load an entire dataset into memory at once — a full-site export, a large CSV import, a bulk search-and-replace — are naturally memory-hungry in a way that normal page loads are not. These are also usually one-off tasks, which makes a temporary limit increase specifically for that operation the right tool, rather than raising the site's default limit permanently.

3. Large images being processed on the fly

Generating thumbnails or resizing images that are unusually large in their original dimensions can use far more memory than the final file size suggests, because image libraries typically need the entire uncompressed image held in memory while processing it. If the error correlates with uploading or displaying images, this is worth checking specifically.

4. A genuine memory leak in custom or poorly written code

A loop that keeps appending to an array without ever releasing anything, or a recursive function with no proper exit condition, will consume memory without bound until the limit is hit — and raising the limit only delays the same failure rather than preventing it. If the error keeps recurring at successively higher limits, this is the explanation to look for, and it needs a code fix rather than a configuration change.

Confirming the fix without leaving anything exposed

If you turned on error display to see the exact message and file involved, switch it back off once you have what you need — a live site should never show error detail to visitors. See debugging a blank white page for the exact settings to toggle and turn off again.

If raising the limit doesn't resolve it

Note the exact error message, including both numbers, and what action triggers it. That detail, along with which plugins are active, is what lets support tell you quickly whether you're looking at a legitimate need for more memory or a script worth investigating further.

Frequently asked questions

Is raising the memory limit a real fix?

It is a real fix when the underlying task genuinely needs more memory than the default allows — a large image, a big import, a data-heavy plugin doing legitimate work. It is only a temporary patch when something is using far more memory than it should, in which case the same error tends to return at the new, higher limit once the underlying script does a little more work.

How do I know how much memory is actually needed?

The error message itself gives the figure it tried and failed to allocate. If that number is only slightly above the current limit, a modest increase is enough. If a script is asking for hundreds of megabytes more than the current limit provides, that points more towards a plugin or script doing something inefficient than towards a limit that is simply too conservative.

Related reading