A machine-readable guide to this site for AI agents and language models is available at/llms.txt(https://dev.acquia.com/llms.txt on the production site).
An Allowed memory size of xxxxxx bytes exhausted error can occur when you access a Drupal page or run Drush. For example:
Error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 8192 bytes) in /home/meosch/l2m.ch/releases/20171229-022429/docroot/core/lib/Drupal/Core/Extension/InfoParserDynamic.php, line 22This error occurs when Drupal or Drush tries to use more memory than the amount allocated in the php.ini configuration file. For Drush with PHP 8.x, the php.ini file is typically located in /usr/local/php8.x/etc/cli/php.ini. The memory_limit parameter defines the allocated memory amount. The following is an example of the setting in the php.ini file:
memory_limit=512MSome pages on your Drupal website, such as /admin or /reports, require additional resources. However, increasing these resources globally can decrease overall site performance.
Instead of making global changes, conditionally increase resources in the settings.php file of your website by adding a code snippet for the memory change in the PHP settings region, under other PHP ini_set() calls. For Drupal applications, the associated Drush tool might also require a memory increase.
ini_set. A global ini_set located later in the file overrides any preceding changes.settings.php file that exceeds the limit used to calculate the available PHP processes, you risk making your entire website unavailable, rather than only the page displaying the out-of-memory error.To modify the memory_limit value for Drupal, use the PHP ini_set() function in the settings.php file of your website.
The following code snippet determines whether the current path is an admin path, increases the memory limit for the admin path, and leaves the default memory limit for other pages:
if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], 'admin') !== false ) {
ini_set('memory_limit', '256M');
}A more complex example sets memory limits for multiple pages. To perform tasks that are not on the /admin/... path, such as tasks to add or edit content, conditionally increase the memory to 256M on the /node/... paths that contain add or edit, as well as on the admin paths, with the following code snippet:
if (isset($_SERVER['REQUEST_URI']) && (
strpos($_SERVER['REQUEST_URI'], '/admin') !== false ||
strpos($_SERVER['REQUEST_URI'], '/node/add') !== false ||
preg_match('#^/node/\d+/edit#', $_SERVER['REQUEST_URI']) === 1
)) {
ini_set('memory_limit', '256M');
}If you receive an out-of-memory error when you upload a file to a specific field through AJAX, increase the memory limit for the AJAX path with a code snippet similar to the following:
if (
isset($_SERVER['REQUEST_URI']) &&
strpos($_SERVER['REQUEST_URI'], 'file/ajax/field_thumbnail_image/') !== false
) {
ini_set('memory_limit', '256M');
}The following example separates the code into more maintainable pieces, so that you can easily add and remove paths by defining them in a variable that is passed to the memory limit function:
$admin_paths = array(
'admin',
'crop',
'node/add',
'node/%node/edit',
);
// standardize node edit paths for validation
$current_path = preg_replace("/\d+/", '%node', $_SERVER['REQUEST_URI']);
foreach ($admin_paths as $admin_path) {
if (strpos($current_path, $admin_path) === 0) {
ini_set('memory_limit', '256M');
}
}For websites hosted on Site Factory, you cannot directly modify the settings.php files. Instead, you can use Factory hooks by adding the previous conditional memory code into a PHP file in the factory-hooks/pre-settings-php directory. For more information, visit Hooks in Site Factory and this example on increasing the memory limit on Site Factory.
Although Acquia recommends that you tune the memory use of your website for specific paths, the specific reasons for how this can be helpful depend on the hosting environment of your website.
If you host your website on Cloud Platform, updating the limit for the entire website takes priority over your server tuning and the PHP memory limit set in the Cloud Platform user interface. When Acquia configures the server tuning, Acquia calculates how many PHP processes can run simultaneously on the server, given the available memory on that hardware with each PHP process that uses the maximum amount of memory allocated for PHP.
Although PHP processes do not 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 state. The Linux kernel then attempts to kill the processes that consume memory to resolve the issue. These processes can include MySQL, which can cause outages for your website and any websites that share your server.
When you limit the memory increase to specific paths, you significantly reduce this risk. However, you must still keep the conditional memory increases as low as possible to reduce the risk that multiple requests for these paths are greater than the total memory capacity of the server.
Memory limit policy is a base module to override the default PHP memory_limit based on various constraints.
The memory limit to run a Drush command can be changed by overriding different settings. To verify that the memory limit is set to the intended value, confirm your memory limit settings for Drush with the following command to retrieve the PHP configuration value:
drush php:eval 'print(ini_get("memory_limit")."\n");'For more information, refer to PHP ini_get().
The php -i command does not display the correct values for memory_limit because the command line PHP uses a different configuration.
The memory limit can be set at the command line by modifying the settings.php file of the Drupal application.
Override the memory used for Drush operations as a one-time override when running the Drush command, or a permanent override in settings.php.
Implement an override by using the PHP CLI "-d" option:
# Set the Drush memory limit to 1024M
# NOTE: This will only work if you do NOT have other memory overrides set up.
php -d memory_limit=1024M /mnt/www/html/path/to/drush <command>
# For example, for examplesite.prod using PHP 8.2 and vendored drush
# the command would look like:
php -d memory_limit=1024M /var/www/html/examplesite.prod/vendor/drush/drush/drush <command>This works only if you do not have other memory overrides set up.
Drush was completely rewritten in version 9 and the options to set memory settings for your PHP environment were removed. Instead of changing the Drush configuration, increase the memory for PHP command-line tools, such as Drush, by adding a conditional memory limit increase to the settings.php file of your application with the PHP_SAPI constant.
To change the memory limit available to Drush commands, add the following code snippet to your settings.php file:
if (PHP_SAPI === 'cli') {
ini_set('memory_limit', '512M');
}