I run into PHP memory limit errors on two pages in the admin area of my site - a massive menu admin page and sometimes on the admin/content/node page. I know how to increase the memory limit globally, but is it possible to somehow increase it only on certain pages?

The site gets a lot of traffic, so it wouldn't be optimal to raise the memory limit all the time since that can reduce the number of requests we can handle.

Thanks!

Comments

hey_germano’s picture

Finally found a way to make this work. Maybe somebody else will find it useful. I added this to settings.php:

$admin = FALSE;
if (preg_match('/admin/', $_SERVER['REQUEST_URI'])) {
  $admin = TRUE;
}
if ($admin) {
  ini_set('memory_limit', '256M');
} else {
  ini_set('memory_limit', '128M');
}
miceno’s picture

I had the same issue and I solved it with your snippet! I have improved it a little bit to include a couple of defines:

<?php
$admin = FALSE;
if (preg_match('/admin/', $_SERVER['REQUEST_URI'])) {
  $admin = TRUE;
}
DEFINE( 'DRUPAL_ADMIN_MEMORY_LIMIT', '48M');
DEFINE( 'DRUPAL_USER_MEMORY_LIMIT', '30M');
if ($admin) {
  ini_set('memory_limit', DRUPAL_ADMIN_MEMORY_LIMIT);
} else {
  ini_set('memory_limit', DRUPAL_USER_MEMORY_LIMIT);
}

?>
diamondsea’s picture

This example is unsafe.

You should use a regular expression that checks that /admin/ is at the START of the string. 

Otherwise attackers could use up your extra memory by making a path like /any-page/path/admin/ and sending lots of requests to exhaust your system's memory.

A better expression to use would be '!^/admin/!', such as:

if (preg_match('!^/admin/!', $_SERVER['REQUEST_URI'])) {
  ini_set('memory_limit', '512M');
}
iaugur’s picture

I would advise using a delimiter such as # not ! - which although perfectly valid in your example looks like a negation rather than a delimiter.

<?php
if (preg_match('#^/admin/#', $_SERVER['REQUEST_URI'])) {
  ini_set('memory_limit', '512M');
}

George Boobyer
www.blue-bag.com

diamondsea’s picture

I agree with you.  Good tip!

Old habits die hard ;-)