How to Increase PHP Memory Limit

2015-01-24 01:26 Posted by: Davis Paul in PHP, Tutorials Go to Comment

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:

view-php-memory-limit

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:

Related Articles

5 Brilliant Ideas for Holiday Email Marketing
12/16
With a growing number of people buying online, making a solid email marketing plan can help take advantage of this trend and bring you with merry and bright holiday sales.
How to use Amazon S3 and CloudFront CDN for WordPress
12/16
AWS (Amazon Web Services) have managed to leave their impact in the field of cloud computing specifically because their s3 and cloud front services. Amazon s3 and CloudFront plug-in helps to implement these services quickly.
Pros and Cons of Hybrid CDNs
12/16
Implementation of hybrid involves a number of advantages and risks too. Here we present various advantages of Hybrid CDNs along with the risks involved in them.
How Your Domain Name Will Impact Seo & Social Media Marketing
09/26
Selecting a domain name is one of the most imperative decisions that you make. Here are some factors that say how important is your domain name for SEO and SMM.
Best Selling Website Templates In 2016
09/20
If you want to be the best then you must not only give the best products and services but also make a website that is inviting and is capable of showcasing your expertise and class. This can be easily done by using one of the various templates available in the market to start right away with your website.
Comment
Be the first to comment!
About the author
Views: 196154
Articles: 93

Author's Hot Articles

godaddy
hub

Hot Posts

Service Recommendation

Cloud Hosting
Cloud hosting is the fastest growing hosting solutions in the industry. »
Cloud Storage
Best Cloud Storage are named based on customer and expert reviews. »
SSL
The most popular SSL certificate providers in the industry are here. »
China Web Hosting
Find out a comprehensive evaluation to the host service provider in China. »
bluehost
Chat Advertising cooperation TOP