PHP memory_limit is just that – a memory limit. But this PHP setting is per-script. You can relate this to a highway’s speed limit, which is also per-vehicle.
That means that if you have a PHP memory_limit setting of 128 MB, each script can only eat as much as 128 MB of the server’s memory.
note: if you just want to learn how to increase the memory_limit, scroll to the end of this article. But I urge you to read everything as it might be useful.
For example, four scripts, can each eat up to 128 MB of the server’s memory. A memory_limit of 128 MB does not mean that the four scripts combined can use up to 128 MB of memory. Being a per-script setting each script can eat this much.
This setting is basically a fail-safe in order to prevent any PHP script to use all your available memory on a server. And if a script goes over that limit, you will see errors like:
Fatal error: Allowed memory size of x bytes exhausted (tried to allocate x bytes) in /path/to/php/script
or like this:
PHP Fatal error: Out of memory (allocated x) (tried to allocate x bytes) in /path/to/php/script
That just means that the script you just ran, ate so much memory, it went over the memory_limit setting.
People refer to this error differently. Sometimes based on what CMS they are using. WordPress users might relate this to the ‘WordPress memory exhausted error. It’s the same thing. Your CMS went over the memory_limit setting.
So I just have to increase the PHP memory_limit, right?
Well, yes. And no.
Let me explain.
Depending on your application, you might need to increase the memory_limit. But try to be conservative in order to avoid exhausting the actual memory on your server.
You should only increase the PHP memory_limit by steps. If you currently have a setting of 128 MB don’t just put 1 GB and leave it be. Increase it to 256 MB first.
The other alternative is to optimize your PHP script to eat less memory. This might prove to be quite a time consuming and actually expensive task if you are not a PHP developer yourself.
If you are not a PHP developer I’d urge you to simply change the plugin, theme, or whatever script is using up a lot of memory.
Why? Because that’s the irony: you need a lot of system memory if you cannot optimize the script. But let’s face it: if you don’t have the budget for a PHP developer you probably don’t have the budget for a powerful server (or hosting plan).
How much PHP memory_limit a CMS (like WordPress, Prestashop or Magento) or PHP app recommends, should tell you a lot about your server and hosting needs.
Let’s take two common examples: WordPress and Magento 2. WordPress works great with 256 MB memory_limit. Magento 2 literally states at least 756 MB with 3-4 GB for testing.
We can fairly assume just by knowing this that a 1 GB RAM VPS will not suffice for a Magento 2 store (well, not a fast one. It will be a pain, you have been warned. Don’t even think about going to production with a Magento store that has just 1 GB RAM available). But it will work just great for a neat WordPress Blog with a decent theme and a conservative amount of plugins and a good caching plugin.
How to increase PHP memory_limit?
We’ll go through the various methods you can increase the PHP memory_limit, based on your server and hosting plan and even location or control panel. I’ll try to make these useful. Feel free to add more in the comments if you didn’t see your case here.
How to change PHP memory_limit in Cpanel?
Log in into your Cpanel account, and find the ‘Select PHP Version’ menu. Click on it.
On the next screen, press ‘Switch to PHP options’. There you will find a selector for PHP memory_limit.
Please note that depending of your Cpanel configuration things may look different or this option might not even exist. In this case try the below methods for VPS or contact your hosting provider. They usually help with stuff like this for free.
How to change PHP memory_limit in Plesk?
Log in into your Plesk account, and go to Domains, choose your website and click on PHP Settings.
There you will find the option to change the PHP memory_limit right under the Performance Settings title.
How to change PHP memory_limit in Webuzo?
In Webuzo, after you’ve logged in, you can click on PHP.
You will need to choose your PHP version (you can find it out using the method at the end of this article). After that, in that php.ini file just search for the memory_limit line (CTRL+F is your friend) and change it to your desired value.
How to change PHP memory_limit on your VPS or server?
Every server has one or several php.ini files, based on it’s configuration. You just need to find the appropiate php.ini file to edit and change the memory_limit value.
To find out the location of the php.ini you can SSH to your server and run one of these commands:
find / -iname php.ini
php -i | grep php.ini
The above commands will output the location of your php.ini files. Just navigate to that location and edit the file as required. Make a backup first just in case.
How to change PHP memory_limit with .htaccess?
I don’t recommend doing this, but it works. The reason why I don’t recommend this is that Apache is faster when .htaccess files are disabled. But sometimes you don’t have so much control over a system.
Anyway, add this to the top of your .htaccess file in the root of your website, in order to increase the memory_limit:
php_value memory_limit 256M
How to change PHP memory_limit in WordPress?
In WordPress, you might need to actually change another thing, after you’ve changed your server’s PHP memory_limit.
In your wp-config.php file, right in the root of your WordPress installation, you need to add the following right before the line that says ‘That’s all, stop editing! Happy blogging.’:
define( 'WP_MEMORY_LIMIT', '256M' );
Now save the file and check if the memory problem has been solved.
How to check the current PHP version and PHP memory_limit?
Create a file named info.php with the following contents:
<?php phpinfo(); ?>
Upload it to your document root. That might be the folder public_html or htdocs in your server or hosting’s control panel file manager.
Now in your browser, enter yourdomain.com/info.php.
What you’ll see is all the PHP information on your server. Just hit CTRL + F, type memory_limit, and presto! You just found out your server’s PHP memory_limit.
Below is a screenshot of what it looks like.
So now you know more about this PHP memory_limit setting, how it works and how to change it. If you liked this article you’ll love my article about php realpath_cache_size
If this article was useful, share it! Maybe someone will find it useful too.
Tech jack of all trades. Programmer, sysadmin and a general tech enthusiast. I work with a lot of stuff, and love learning and talking about new or interesting stuff.