Increase PHP memory limit

Last modified: October 15, 2009 - 12:25

A PHP memory limit of 16MB is the minimum requirement for Drupal 6 and 32MB is recommended. Some sites may need more than 32MB if they are using certain contributed modules such as CCK and Views. Memory limits of 64MB and higher are not unusual. There are several techniques to increase the PHP memory limit and you only need to use one of them. The right one for you depends on your system configuration.

You may try to install a module

List of modules which allow you increase your PHP memory limit without editing any files:
Drupal Tweaks

php.ini

This is the recommended approach if you have access to php.ini. This may not be possible in many shared hosting environments. Note that this change will affect all websites and PHP scripts on the server.

  1. Locate the php.ini file used by your web server. You can use the phpinfo() PHP function to find it. During installation Drupal checks the PHP Memory Limit and if it is less than 16M an error message also provides the path to the php.ini file.
  2. Edit the memory_limit parameter in the php.ini file (usually in a section called Resource Limits)
    memory_limit = 32M  ; Maximum amount of memory a script may consume (32MB)
    If there is no section already for this, place the above line at the end of the file.
  3. Restart Apache.

Note: If you are using XAMPP/WAMP, there may be two PHP.ini files (one under the PHP directory and the other under Apache/bin). To change your memory limit, edit the file in the XAMPP/Apache/bin directory.

The next two solutions are more restricted in scope and, in some cases, may be more appropriate choices than affecting all sites.

.htaccess

Edit the .htaccess file in the Drupal root directory. Look for the section:

# Override PHP settings. More in sites/default/settings.php
# but the following cannot be changed at runtime.

and immediately after this add the following line:

php_value memory_limit 32M

settings.php

If Drupal is already installed, you can edit sites/default/settings.php. This method will affect only the site using this file.

Locate the PHP settings section and add the following line at the end of that section:

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

Shared Hosting

In some shared hosting environments, access to the PHP memory limit setting is restricted. If you cannot make the change yourself, please ask your hosting provider to adjust it for you, or look for a new host that allows more flexibility.

Check your change has taken effect

In all cases, it pays to ensure that your change is actually working. Use phpinfo to verify that your memory actually is what you want it to be. If your change doesn't seem to be working, double-check the location of php.ini displayed in the phpinfo page. Some systems have multiple copies of that file in different places. Only one is used and the others are red herrings.

Finding php.ini on your Local Server

  • For MAMP see http://drupal.org/node/66187 particularly the "Optional: Adjust PHP's memory limit for scripts" section (note the difference for MAMP Pro).

Current PHP memory-limit is already at least 16M

Martin.Schwenke - March 8, 2009 - 06:34

The PHP documentation says that for PHP 5.2.0 the default value for memory-limit was increased to 16M and the current default now seems to be 128M. I know this might not be true for those in a shared hosting environment where the provider may have decreased memory-limit from its default value or for those still running PHP4.

However, perhaps this page should give more prominence to checking the current value before "increasing" the value? ;-)

PHP memory limit: Specific increments to add?

Ashford - March 8, 2009 - 21:40

Referring to: ini_set('memory_limit', '16M');

Should the memory_limit be increased in any specific increments to test how much will work for your installed modules?

For example, first try 16M.
If you still get the exhausted memory error, try 32M.
Next, 64M.
Next, 128M.

Ashford

When I add

Milkrow - May 6, 2009 - 04:05

When I add "ini_set('memory_limit', '16M');" to my settings.php file, the whole site goes blank. I'm not sure why this is happening. This issue is occurring also when I add a new module. I checked to make sure I had not exceeded the storage capacity of the account and I've got plenty of room. Any other ideas what could be causing the site to drop out like this?

WSOD. try increasing the

alex.ihlo - May 7, 2009 - 17:56

WSOD. try increasing the memory limit. I run 96 cuz one of my modules asks for it. I think 32 is the most widely used though.

Alexander Ihlo

Increase Memory for PHP Version

bluewebco - August 21, 2009 - 15:41

You may experience a 500 Internal Server Error if you follow the basic instructions on a linux/apache setup. You may need to do what I did, and enclose the setting within the appropriate PHP version clause:

# PHP 5, Apache 1 and 2.
<IfModule mod_php5.c>
php_value memory_limit 16M
</IfModule>

GoDaddy php5.ini

christo - July 11, 2009 - 09:08

Just a note: on GoDaddy, you're likely running PHP 5.2.x with your account, particularly if running Drupal 6.x, and definitely with the full setup with Ubercart. In this case, you need to have php5.ini, not php.ini in the root of your hosting account. With GoDaddy, you can set the PHP memory up with memory_limit = in your php5.ini file.

To see all the settings, create a page named something like phpinfo.php containing the following:

-------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Info</title>
</head>

<body>
<?php phpinfo(); ?>
</body>
</html>
----------------------------------------

Upload that to your hosting account and browse to it, and a lot of questions get answered.

A Further Note on GoDaddy's php5.ini file

gurustump - September 10, 2009 - 07:58

If you need to change your memory limit in a GoDaddy Shared Hosting Account, you need only create your own php5.ini file in the root directory of your hosting account. This file does not need to contain absolutely every setting in the originaly php5.ini file that is running on your site. You only have to include your own overrides. So for example, if you wanted to adjust your memory limit from 64MB (which I believe is the GoDaddy default) up to 256MB, you would create a text file named "php5.ini" in your root directory. You would then put the following line of code in it:

memory_limit = 256M

And that's it. If you already did as christo suggested and created that phpinfo.php file, you can now use your browser to go to it, and in the memory_limit line it will show you're up to 256MB (incidentally, in my Drupal install at GoDaddy, for which I used their automated script to originally install, there was already a phpinfo.php file in the root directory of my Drupal site...I'm under the impression it's a default part of a fresh Drupal 6 install).

There is a very short article in GoDaddy's help pages that will tell you nothing new about this, but will at least confirm what christo and I are saying here:

http://help.godaddy.com/article/1085

As you can see from that article, if for some reason you are using PHP 4 in your GoDaddy hosting account and wish to change some php.ini settings, you would name your file php.ini instead of php5.ini.

Raising memory limit on Bluehost (maybe others)

zhahai - August 12, 2009 - 03:51

Drupal 6.13 with various contrib modules exceeded 32MB accessing /admin/build/modules after enabling one more module. (It all worked under 5.19)

Nasty effect, as I can't get into /admin/build/modules to disable the module!

Q) Is there a way to disable modules when you cannot get to that page? Perhaps via the database? Or is your site really hosed if you really cannot increase the memory limit?

Anyway, here's what worked on bluehost.com, and might on some other hosts. In the top web folder (public_html), there was a php.ini file. This is actualy one level above the folders where I actually host sites. Internal documentation says to copy any edited version of this php.ini to "any folder containing php to be executed". I copied the edited php.ini to the root for a given Drupal site (public_html/drupalfolder/ - which is where the subdomain drupalsite.mydomain.com lands) and it worked. Whew!

Most of drupal seems to execute out of its root folder, with code from various subfolders being include-ed but not executing separately, so maybe that folder is enough.

NOTE: First I edited the whole php.ini and copied that, but this file had register_globals enabled! I wasn't sure what else might be misconfigured, so I made a php.ini file in the drupal root folder which contained only the resource section (size and time), hoping that all other values would be inherited from the master php.ini whch seems to work for Drupal and other sites I host - so I wouldn't have to go through the local php.ini file line by line and figure out which options were bad. This seems to work so far.

This worked for me (on Bluehost)

robzpanther - October 2, 2009 - 21:16

This is the only solution that I've been able to get to work correctly. Everything is running well so far.

My installation doesn't even exist as a subdomain and it still updated correctly just after dropping it back into the root folder.

BTW, I use the same method as MrRudolph to remove unwanted modules. I usually disable a few more (if not all) and run update.php afterwords.

PHP has a max limit as well

ald_jl - October 6, 2009 - 21:09

I discovered that PHP has a maximum memory limit that you can allocate. If you go over it, it will use it's default value, while still telling you it is allocating the limit you have specified.

I had a large data import project, so I set my php memory limit to 2048M.

I kept getting the dreaded "Allowed memory size exhausted" error, even after a server reboot.

I finally dropped my limit in increments to 1024M, and everything worked correctly.

This is a very temporary situation, but might not be that uncommon for projects like mine where large amounts of data are being migrated.

.htaccess Edit the .htaccess

kidfgrk - October 13, 2009 - 11:15

.htaccess
Edit the .htaccess file in the Drupal root directory. Look for the section:

# Override PHP settings. More in sites/default/settings.php
# but the following cannot be changed at runtime.
and immediately after this add the following line:

php_value memory_limit 16M

that worked from me

thanx

Memory increased to 32, 64 and 124 - Problem persists

pekman - October 21, 2009 - 04:29

I have worked together with my web host to increase the memory limit. First, I increased it to 32MB. Then to 64MB and finally to 124MB. However, I am still getting an error when I run cron.

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 8621231 bytes) in /public_html/xxx/modules/search/search.module on line 444

I think this started happening after I installed the Activity Stream module (but I am not sure). I will try to uninstall it to see if that is the problem. But if anyone has solved this, please help. This has been going on for the last few days. Thanks.

>UPDATE: I disabled Activity Stream and the problem is still there, so that is not the issue.

Clueless...

thealchemist - October 26, 2009 - 00:34

Afters a week of searching and trying at least a dozen "solutions" I have not come closer to solving my initial fatal memory error. If I increase memory_limit in a php.ini file to anything higher than 32M I get among others a packet error. If I add stuff to my .htaccess file I get a 500 error loading my site. When I try the settings.php way I also get a 500 error. I am on shared hosting and have a standard 32M limit.

I have even tried increasing the memory limit with the php.ini and then attempted to solve the other isssues with my.conf / my.cnf but again nothing. I have asked for suggestions here http://drupal.org/node/570678 but no luck so far. Could it have anything to do with mod_php / FastCGI?

Same issue

SmokinJ - November 6, 2009 - 17:19

If I try adding the changes in htaccess file I get a 500 error. I tried making the suggested change in the settings.php file and when I try installing the civicrm module it says my memory is set at 18M. Any ideas?

As per docs on this page:

dman - November 7, 2009 - 01:24

The ability to adjust php settings can be locked down by your host, and often is (for good reason) on shared hosting. If the methods described do not work - this is almost certainly the case.

In some shared hosting environments, access to the PHP memory limit setting is restricted. If you cannot make the change yourself, please ask your hosting provider to adjust it for you, or look for a new host that allows more flexibility.

.dan.

As per docs on this page:

dman - November 7, 2009 - 01:25

The ability to adjust php settings can be locked down by your host, and often is (for good reason) on shared hosting. If the methods described do not work - this is almost certainly the case.

In some shared hosting environments, access to the PHP memory limit setting is restricted. If you cannot make the change yourself, please ask your hosting provider to adjust it for you, or look for a new host that allows more flexibility.

.dan.

 
 

Drupal is a registered trademark of Dries Buytaert.