Friday, 21 March 2014

How to Increase PHP Memory Limit

How to Increase PHP Memory Limit When you saw an error like “Fatal Error: Allowed memory size of xxxxxx bytes exhausted” either in browser or server logs, it means PHP had exhausted the max memory limit. Mostly, this is due to the insufficient memory allocated for PHP script. And in this article, we will talk about how to increase the memory limit of PHP.
Check PHP memory_limit
To see how much memory allocated for you php, you need to create a file on your server (like view_php_info.php), and put following code in it:
<?php phpinfo();?>
Visit it 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:

Also, you can use this way to verify the memory limit change
How much memory do I need?
It depends, for wordpress core, 32MB is the default memory limit. 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 128MB or more.

Increase PHP memory_limit

Way1: Modify php.ini
The most easy and common way is to change the configuration in php.ini.(For shared hosting use, please skip this method) 1)Firstly you need find your php.ini .If you did what the last section let you ( create a php file to view php config ), then you can just visit that page, and search for “Loaded Configuration File”, see below:
Otherwise, for linux user, you can simple use “php -i | grep Loaded Configuration File” to find it.For windows user, you can try to find it in your php install directory.
2)Edit php.ini .Search “memory_limit” in your php.ini, and change 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 ApacheUse follow command to restart apache:
httpd restart
For some restricted environment, such as shared hosting user, it’s impossible to change php.ini, you’ll 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
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 be.
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, there is another choice, 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');

No comments:

Post a Comment