How to Increase PHP Memory Limit

Davis Paul| 2015-01-24 PHP, Tutorials Comments( 0 )

When you saw an error like “Fatal Error: Allowed memory size of xxxxxx bytes exhausted” in either browser or server logs, it means PHP had hit its memory limit. Mostly, this is due to the insufficient memory allocated for PHP script. And in below, we will talk about how to increase the momory limit of PHP.

Check PHP memory_limit

To know the value of PHP memory_limit, you can check the configuration in PHP.ini. However, sometimes you might not know where php.ini is located. So, the simplest way is to use execute PHP function phpinfo(), which will display all the php related server configuration.

To do so, you need to create a PHP file (we can name it as view-php-info.php), and put following code in it:

<?php phpinfo(); ?>

Upload the file to your web server, and access the file in your browser, you'll see a table which lists out all the config about current php environment. Search for "memory_limit", you'll see:


In above case, we set the PHP memory_limit to 128M in our testing environment.

By the way, you can use this method to verify the change of PHP memory limit.

How much memory do I need?

It depends, for wordpress core, 32MB is good enough. For drupal6 core, the min memory limit is 16MB, 32MB is recommended. But if you install many plugins, especially the plugins deal with image, you probably need a memory with 64MB or more.

Increase PHP memory_limit

Way1: Modify php.ini

The most easy and common way is to change the configuration in php.ini

  1. Firstly you need find your php.ini . In the report generated by phpinfo() (you can use PHP script created in above), there is a parameter called "Loaded Configuration File" (see below), that's where your PHP.ini file is located. If you use Linux, you can simple run the command "php -i | grep Loaded Configuration File" to find it.For windows user, you can find it in your php install directory.
  2. Edit php.ini .Search "memory_limit" in your php.ini, and change the value of it. If no "memory_limit" found, add the following line at the end of php.ini
    memory_limit = 128M ; /* Change the 128M to your needs */

    Save file.

  3. Restart Apache. Use follow command to restart apache:
    httpd restart

For some restricted environment, such as shared hosting user, your web host might not allow you to php.ini directly, then you could consider the follow ways.

Way2: .htaccess

Notice: This method will only work if PHP is running as an Apache module.

Find the ".htaccess" in your root directory of the specified domain, if there isn't, create one. Put the following line in it.

php_value memory_limit 128M; /* Change the 128M to your needs */

Way3: Change Memory At Runtime

Right, PHP memory limit could be changed at runtime. Via this way, you have no need to change and configuration of your server environment.

For Wordpress Users

In your wp-config.php, find or add the following line:

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

WordPress will automatically check if PHP has been allocated less memory than the entered value before utilizing this function. For example, if PHP has been allocated 64MB, there is no need to set this value to 64M as WordPress will automatically use all 64MB if need.

Notice: By default, WordPress will attempt to increase memory allocated to PHP to 32MB (code is at beginning of wp-settings.php), so the setting in wp-config.php should reflect something higher than 32MB.

For Drupal Users

For drupal, you can edit sites/default/settings.php. Locate the PHP settings section and add the following line at the end of that section:

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

This method will affect only the site using this file.

For other frameworks

Similarly to Drupal, in your code, you can add the following line in your php code. That will take effect at runtime.

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

Increase Failed

There are many reasons that can lead to increase failed. If you are a shared hosting user, the most possible reason is that your hosting vendor does not allow for increasing the PHP memory limit, in this case, contact your host to increase the PHP memory limit. Some web sites might reject to increase PHP memory limit since this means your site require more server resource than normal. If this happens, then you can consider to change your php hosting to those who allows for large PHP memory limit.

Tagged with:

We hope webmasters to share and promote the good articles ,Please click herecontribute

You need to log in to commentLogin|Register

Be the first to comment!