Guide PHP & Development

How to increase the PHP memory limit

Where memory_limit is set, how to raise it safely, and why a higher number is not always the actual fix for an exhausted-memory error.

Updated 6 min read Beginner

The memory_limit directive caps how much RAM a single PHP script is allowed to use before PHP kills it. Hit the ceiling and you get an error that names itself plainly:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate ...)

The number is a byte count — 134217728 bytes is 128MB — so the error is telling you the exact ceiling that was in place at the moment. Raising the limit clears the symptom quickly. Whether it is the right fix depends on why the script needed that much memory in the first place, which is worth thirty seconds of thought before you reach for a bigger number.

Where memory_limit is set

On shared hosting you have two routes, and you do not need root access for either:

  • The PHP settings area of your control panel. This is the normal route. It lists the current value and lets you pick a higher one from a dropdown or type a value directly, scoped to the site and PHP version you are editing.
  • A user-level php.ini or .user.ini file in the site's own directory, if your control panel supports custom ini overrides. A line in that file looks like this:
memory_limit = 256M
.htaccess is not a reliable way to set this

On a PHP-FPM setup — which is what most modern shared hosting runs, see what PHP-FPM is — a php_value line in .htaccess for a directive like memory_limit is either ignored or causes a 500 error, because FPM does not read Apache-style ini directives from .htaccess the way the older mod_php handler did. Use the control panel or a user ini file instead.

Choosing a value

SituationReasonable starting point
A small custom script or API endpoint128M is usually already enough
WordPress with a moderate number of plugins256M
WordPress admin tasks — imports, some page builders, bulk media processing512M
A large data import or export job512M–1024M, ideally run as a scheduled script rather than through a browser request

Set the lowest value that clears the error rather than jumping straight to the largest number available. A script that only needed 200MB and was given 1024M has not become more efficient — it has just been allowed to hide a problem for longer before it eventually hits a ceiling somewhere else, such as the server's total available memory under real traffic.

When raising the limit is not the actual fix

A script that steadily climbs towards whatever limit you give it, rather than settling under a stable figure, usually has a memory leak or is loading far more data than it needs — pulling an entire database table into an array instead of paging through it, for instance. Doubling the limit against that kind of script buys a little time and then fails again at the new ceiling. If you keep raising the number and keep hitting it, that is the signal to look at what the code is actually doing rather than at the setting.

Check the error log for the real cause

The "allowed memory size exhausted" message on screen is not always the whole story. Read the error log for the line number and file where the allocation failed — that tells you which specific operation is the memory-hungry one, rather than leaving you guessing across an entire plugin or codebase.

Applications with their own memory setting

Some applications add a second ceiling on top of PHP's own. WordPress, for example, applies its own internal limit through WP_MEMORY_LIMIT in wp-config.php, and that value cannot exceed whatever memory_limit is set to at the PHP level — raising it in wp-config.php alone does nothing if the underlying PHP setting is still lower. Change the PHP-level setting first, then raise the application's own figure to match if it needs one.

Confirming the new value took effect

After changing the setting, confirm it applied before assuming the job is done:

<?php echo ini_get('memory_limit');

If this still shows the old value, the most common causes are editing the wrong PHP version — a site can have more than one available, see checking your PHP version — or a setting further down the priority order overriding what you just changed, such as a value hard-coded in the application itself with ini_set() at runtime, which takes effect after the server-level setting and can override it for that request.

If a script is also timing out rather than running out of memory, that is a separate limit — see increasing PHP max execution time — and the two are frequently raised together for the same import or export job.

Related reading