1. Home
  2. Drupal
  3. How to change PHP memory limit in Drupal conditionally

How to change PHP memory limit in Drupal conditionally

Quite often your Drupal website may require additional PHP memory. A typical error “PHP memory exhausted” indicates that your Drupal needs more PHP memory. (such as admin pages or reports) can require additional resources from your hosting environment to run properly. However, increasing these resources globally can decrease your website’s overall performance. For example, if to run a report you increase the amount of memory provided to PHP to 512MB, you decrease the number of PHP processes that the server can allocate before running out of memory, which limits the number of

Rather than updating PHP memory globally, we recommend to only apply the increased PHP memory limit to the pages that need it. For example, administrative pages or reports may require additional resources from your hosting environment to run properly.

The way we recommend to change it (the original copy was posted on Acquia’s documentation pages) is to update your settings.php files as below (we assume you are increasing PHP memory limit to 256MB for URLs ‘admin’, ‘cron’, ‘user’, ‘node/add’, ‘node/*edit’).

Change PHP memory limit conditionally for Drupal 7

$admin_paths = array(
  'admin',
  'cron',
  'user',
  'node/add',
  'node/%node/edit',
);

// standarize node edit paths for validation
$current_path = preg_replace("/\d+/", '%node', $_GET['q']);

foreach ($admin_paths as $admin_path) {
  if (strpos($current_path, $admin_path) === 0) {
    ini_set('memory_limit', '256M');
  }
}

 Change PHP memory limit for admin URLs for Drupal 8

if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/admin/') === 0 ) {
  ini_set('memory_limit', '512M'); 
}

 

Updated on 7 June, 2017

Was this article helpful?

Related Articles