Conditionally increasing memory limits

Some pages on your Drupal website (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 256MB, you decrease the number of PHP processes that the server can allocate before running out of memory, which limits the number of webpages that can be served that don't require additional memory.

Instead of making global changes, you can increase resources conditionally on a case-by-case basis in your website's settings.php file. To do this, add a code snippet that describes your memory change in the PHP settings region, under the other ini_set calls.

 

Examples of conditional memory limit changes

Drupal 8

Here's a code snippet example that works with Drupal 8. It determines if you're on an admin path, and if so, increases the memory limit, leaving the default memory limit in place for other pages on the website:

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

 

Drupal 7

Here's a code snippet example that works with Drupal 7. It determines if you're on an admin path, and if so, increases the memory limit, leaving the default memory limit in place for other pages on the website:

if (isset($_GET['q']) && strpos($_GET['q'], 'admin') === 0) { 
    ini_set('memory_limit', '256M'); 
}

A slightly more complex example of this sets limits for multiple pages. If you're performing tasks that are not on the /admin path (such as adding or editing content) you can conditionally increase the memory on add and edit (as well as the admin paths) to 256M using the following code snippet:

if (
    (strpos($_GET['q'], 'admin') === 0) || strpos($_GET['q'], 'node/add') === 0)
    || (strpos($_GET['q'], 'node/') === 0 && preg_match('/^node\/[\d]+\/edit/', $_GET['q']) === 1)
  ) {
  ini_set('memory_limit', '256M');
}

If you're receiving an out-of-memory error when uploading a file into a specific field using AJAX, you can specifically increase the memory limit for the AJAX path as well by using a code snippet similar to the following:

if (strpos($_GET['q'], 'file/ajax/field_thumbnail_image/') === 0) {
   ini_set('memory_limit', '256M');
}

The following example separates the code into more maintainable pieces, enabling you to add and remove paths more easily by defining them in a variable that is passed to the memory limit function:

$admin_paths = array(
  'admin',
  'crop',
  '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');
  }
}

Optionally, you can install and use Page memory limit module to manage your website's conditional memory limits.

You can also create memory overrides for Drush operations by adding the following code snippet to your website's settings.php file:

Drupal 7

if (drupal_is_cli()) { 
  ini_set('memory_limit', '256M');
}

Drupal 8

if (PHP_SAPI === 'cli') {
  ini_set('memory_limit', '256M');
}

You can confirm your memory settings for Drush by running the following command:

drush php-eval 'print(ini_get("memory_limit")."\n");'

Why set specific paths?

Although Acquia recommends that you tune your website's memory use for specific paths, specific reasons for how this can be helpful depend on your website's hosting environment.

If you host your website on Acquia Cloud, updating the limit for the entire website takes priority over your server tuning and the PHP memory limit set in the Acquia Cloud UI. When Acquia configures server tuning, we calculate how many PHP processes could run simultaneously on the server, given the available memory on that hardware with each PHP process using the maximum amount of memory allocated for PHP.

Although PHP processes do not automatically allocate or consume the full amount of memory set as the PHP memory limit, if the PHP processes attempt to consume more memory than is available on the server hardware, the server as a whole will eventually reach an out-of-memory (OOM) state. The Linux kernel then attempts to kill the processes that are using up memory to resolve the issue. These processes can include MySQL, which can cause outages for your website and any websites sharing your server.

By limiting the memory increase to specific paths, you significantly reduce this risk. However, you should still keep the conditional memory increases as low as possible to reduce the risk that multiple requests for these paths would be greater than the total memory capacity of the server.

Conditional memory increases for Acquia Cloud Site Factory

If your websites are hosted on Acquia Cloud Site Factory, you can't directly modify your websites' settings.php files. Instead, you can use factory hooks. Add the conditional memory code in a PHP file in your factory-hooks/pre-settings-php directory.

For more information, see Executing code before a site loads in the Acquia Cloud Site Factory documentation.

Contact supportStill need assistance? Contact Acquia Support