When a PHP script tries to use more memory than PHP is allowed to give it, the script stops with a fatal error like this:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /home/example/public_html/wp-content/plugins/example/file.php on line 512
The number (here 134217728 bytes, which is 128 MB) is your current memory_limit. This guide shows you how to raise that limit correctly for your site, and, just as importantly, how to tell whether raising it is actually the right fix.
Last reviewed: 27 July 2026, against PHP 8.x (all currently supported branches behave the same for this setting). This guide is written for Noiz hosting and is kept current against PHP. It complements, and does not replace, the official PHP documentation linked below.
Official Documentation Reference
- PHP Manual: the
memory_limitdirective - PHP Manual: per-directory
.user.inifiles - PHP Manual: setting a value at runtime with
ini_set()
Important: the old RLimitMem trick does not work
Older guides tell you to add RLimitMem max to your .htaccess file. Do not use it for this problem. RLimitMem is an Apache directive that limits the operating-system memory of helper processes Apache launches (such as CGI scripts). It has nothing to do with PHP's memory_limit and will not change the value in the error message. Use one of the methods below instead.
First, decide whether to raise the limit at all
The PHP default is 128M (128 MB) and that is enough for most well-behaved sites. A memory error is often a symptom rather than the disease. Before you raise the limit, ask:
- Did this start suddenly? A recent plugin, theme, or code change that suddenly needs far more memory usually points to a bug or a runaway loop, not to a limit that is genuinely too low.
- Is the requested amount tiny? If the script fails while trying to allocate only a few kilobytes (as in the example above), memory has already been used up elsewhere. Raising the limit may only delay the crash.
- Is it a known heavy task? Large image processing, big imports/exports, or a busy WordPress site legitimately need more than
128M. Here, raising the limit is the correct fix.
Raise the limit to a sensible figure such as 256M or 512M. Avoid -1 (unlimited) on shared hosting: a single faulty script could then consume all available memory. If a value like 512M still is not enough, treat that as a strong sign of a code problem and investigate the script rather than raising the number again.
Which method should you use?
How you change the limit depends on how PHP runs on your account. The methods below are ordered from most reliable to most situational. If you are not sure how your site runs PHP, start with the control panel method, which works in every case.
Method 1: Your hosting control panel (recommended)
The most reliable way to change PHP settings is through the PHP settings screen in your hosting control panel. This applies the value at the account level, so it works no matter how PHP is served.
- Plesk: open Websites & Domains, select your domain, click PHP Settings, set memory_limit to your chosen value, then click OK or Apply.
- DirectAdmin: open your account's PHP configuration for the domain and edit the memory_limit value for the active PHP version.
- cPanel: open MultiPHP INI Editor, choose your domain, and set memory_limit in the editor.
If your control panel does not expose a memory_limit field, or you manage the server yourself, use one of the file-based methods below.
Method 2: A .user.ini file (modern PHP-FPM and FastCGI setups)
Most current hosting runs PHP as FPM or FastCGI rather than as an Apache module. On those setups the correct file-based approach is a .user.ini file, not .htaccess.
- Using File Manager or FTP, go to the folder where your site's PHP runs. For most sites this is your document root, for example
public_htmlorhttpdocs. - Create a plain-text file named
.user.ini(note the leading dot). - Add this single line and save:
memory_limit = 256M
Give it a few minutes. PHP caches .user.ini files, so a change can take up to five minutes to take effect. If nothing appears to happen, wait, then reload the page.
Method 3: .htaccess (only when PHP runs as an Apache module)
The classic .htaccess method works only if PHP is loaded as an Apache module (mod_php). On that kind of setup, add this line to the .htaccess file in your document root:
php_value memory_limit 256M
If the error keeps appearing, add the same line to the .htaccess file inside the specific folder named in the error message, for example yourdomain.com/wp-admin/.htaccess.
Watch for a 500 error. If adding php_value makes the whole site return a 500 Internal Server Error, your server is not using mod_php. Remove that line immediately and use Method 1 or Method 2 instead.
Method 4: In the script itself (targeted, temporary)
If you only need more memory for one specific script and you can edit it, add this near the top, before any heavy work runs:
<?php
ini_set('memory_limit', '256M');
This is handy for a one-off import or maintenance script. It will not help if the memory is exhausted before this line is reached, or if the host has locked the setting for security reasons.
How to confirm the new limit is live
Create a small file named meminfo.php in your document root with the following, load it in your browser, and read off the value:
<?php
echo 'memory_limit = ' . ini_get('memory_limit');
Delete this file as soon as you are done so it is not left publicly accessible.
Troubleshooting
- The site returns a 500 error after editing
.htaccess: your PHP is not running as an Apache module. Remove thephp_valueline and use the control panel (Method 1) or a.user.inifile (Method 2). - The value will not change: a
.user.inichange can take up to five minutes because of caching. Check that you edited the file in the correct folder, and that no control panel setting is overriding it. The control panel value generally wins. - It still runs out of memory at a very high limit: a value such as
512Mthat still fails almost always means a code fault, a bad plugin, or an infinite loop. Disable recently added plugins or extensions one at a time to find the cause, rather than raising the number further. - Raising the limit helps briefly, then it fails again on some accounts: shared environments can also cap the total physical memory a single account may use, independently of PHP's
memory_limit. If PHP reports the higher limit but scripts are still being cut off, the account may be hitting that ceiling, and the workload likely needs a plan with more resources.
Still stuck?
If the error will not clear after trying these methods, Noiz support can check exactly how PHP is configured on your account and set the right value for you. Customers on a Noiz managed plan can simply open a support ticket and Noiz will handle it. Include the full error message, the affected URL, and which method you have already tried so the team can help you quickly.
