Increasing Memory for Specific Paths in Drupal 8
Most of the examples I see for Drupal 8 are for single-path memory limit increases.
The examples for Drupal 7 often follow the pattern below.
Here’s a D8 version that supports multiple paths.
Place this in your settings.php (or site/environment-specific settings.php), then update the paths accordingly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Increase memory for specific paths. if (!empty($_SERVER['REQUEST_URI'])) { $memory_hungry_paths = [ '/admin', '/search?keys=', '/node/add', ]; foreach ($memory_hungry_paths as $path) { if (strpos($_SERVER['REQUEST_URI'], $path) === 0) { ini_set('memory_limit', '256M'); } } } |
The options are endless for how you can handle different paths. Here’s another example where we cover node/{NID}/edit paths.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// Increase memory for specific paths. if (!empty($_SERVER['REQUEST_URI'])) { $memory_hungry_paths_strpos = [ '/admin', '/search?keys=', '/node/add', ]; foreach ($memory_hungry_paths_strpos as $path) { if (strpos($_SERVER['REQUEST_URI'], $path) === 0) { ini_set('memory_limit', '256M'); } } $memory_hungry_patterns = [ '/\/node\/[0-9]*\/edit/', ]; foreach ($memory_hungry_patterns as $pattern) { if (preg_match($pattern, $_SERVER['REQUEST_URI'])) { ini_set('memory_limit', '256M'); } } } |
You could combine the node/add and node/{NID}/edit paths with the same regex pattern.
You could handle all of the urls with preg_match if that’s your preference. strpos is more performant, as far as I know.
One Comment
John C
Thank you. This code is so useful. I will use it to increase memory limit for the user upload page and checkout page of my website. Some people had their files not uploaded on those pages before.