Increasing PHP memory limit (setting name:
memory_limit) is usually needed in many situations, especially when certain PHP platform scripts like WordPress tells you that it needs more memory for PHP to run more efficiently.
Fortunately, this is very easy to do. This article will guide you on how to increase PHP memory limit in different ways.
Assuming we want to change the PHP memory limit to 256MB (megabytes):
- Ask your host to adjust it for you.
php.ini, locate and change the
memory_limitconfiguration. Here, we will locate the setting on
php.iniand change the value to:
memory_limit = 256M
Then, restart both
.htaccessfile of the website’s root directory, add the following line:
php_value memory_limit 256M
Then just save the file.
- In the PHP file that you want to have memory limit increased, or the main include file, add this line to the top of the file:
What is PHP memory limit?
PHP memory limit is simply a configuration directive to define on the maximum amount of memory in bytes that a script is allowed to allocate, as in PHP.net. This helps to prevent poorly written PHP script to eat up the server memory, or malicious PHP scripts taking up the server bandwidth.
On the contrary, too low memory limit value can cause issues on the PHP scripts that naturally need to have fair memory allocated for it to run properly.
How to get PHP memory limit?
We have a tutorial article here on how to get the PHP memory limit with multiple methods:
What is good memory limit amount?
In general, 256MB is more than sufficient for any general PHP script to run properly. Scripts need more than that is usually heavy running script (eg. array looping, data scanning, backing up scripts) or scripts that take long time to process.
And also, badly PHP written scripts will consume more memory to process (because they’re bad!). As described above, malware PHP scripts will also take a lot of memory on your server.
So leaving the value too high isn’t really good in most cases, unless you’re doing some development works that needs to have everything unlocked to test your script’s capability, which is fine.
Again, we would suggest 256MB to be set for
memory_limit, which is 256M, which will be more than enough.
Increasing PHP memory limit
The basic syntax on PHP memory limit value is:
…in which M is megabytes and G is gigabytes in memory.
// 128 MB memory_limit = 128M // 1 GB memory_limit = 1G
Below are 4 methods to increase (or decrease) PHP memory limit in details:
1. Ask your host
Sounds funny right? But in most cases, if you use a good (and friendly) web hosting, just ask them to change value for you, and they’ll be more than happy to do that.
Most shared web hosting also have their own PHP memory allowance policy, so letting the provider adjust this value might also be more accurate than you doing it yourself.
On Linux based operating systems, the file is usually located at
/etc/, so the full path is
/etc/php.ini. You can open the file using any text editor, look for the setting name (
memory_limit, or more accurately,
memory_limit =), and replace the value after the
= sign, to the value you wanted. Just remember to follow the syntax, M for megabyes and G for gigabytes.
Then, save the file and restart the Apache (or Nginx), and PHP-FPM services to make sure the new value is applied.
// Restarting Apache (Redhat based OS) systemctl restart httpd // Restarting Apache (Debian based OS) systemctl restart apache2 // Restarting Nginx systemctl restart nginx // Restarting PHP-FPM systemctl restart php-fpm
On Windows, locate the
php.ini file inside your PHP executable folder, depends on the web application you’re using (standalone PHP installation, XAMPP, Laragon, etc.). Usually, the file is located in the same directory as the
php.exe file. Then, replace the value of the
memory_limit configuration inside the file, and restart the web service (Apache or Nginx).
The new PHP memory limit value will work in your PHP application after the services restarted.
To adjust PHP memory limit using
.htaccess file, first, make sure the file is the one on the root directory of your website (because there can be multiple
.htaccess files in sub-directories). Open the file in any text editor, add this line to the top of the file:
php_value memory_limit 256M
Save the file, and the setting will be applied immediately.
**This method might not work if you are using a shared web hosting and your provider disabled the ability to adjust PHP setting using
ini_set in PHP file
Setting PHP memory limit by this method will only apply the configuration to a single file (the file that got edited), so it would be better to do this on a global configuration PHP file that gets included into other files in your PHP application framework.
Open the file, add this line to the top of the file, after
Then save the file, the setting will work immediately.
**This method might not work if you are using a shared web hosting and your provider disabled the
ini_set function. Adding the
@ symbol will hide the error in case the function could not be run.
Conclusion on PHP memory limit
Only give PHP the fair amount of memory it needs. 16MB is too low for nowadays PHP scripts, and also, it doesn’t make sense to give a 2GB of memory limit on a sever with only 1GB of ram. So we recommend 256MB setting value, which is good for most cases.