From 2e758ad4060ed76fab7823a74cf557bd81d1585d Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Wed, 5 Mar 2025 08:53:15 +0000
Subject: [PATCH 1/2] Use LRU for entity cache

---
 core/core.services.yml                         | 5 +++--
 core/modules/migrate/src/MigrateExecutable.php | 3 ---
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/core/core.services.yml b/core/core.services.yml
index bb73e29d505b..1ee586a6d607 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -8,6 +8,7 @@ parameters:
   # function properly before that runs.
   cache_default_bin_backends: []
   memory_cache_default_bin_backends: []
+  entity.memory_cache.slots: 1000
   security.enable_super_user: true
   core.moved_classes:
     'Drupal\Core\StringTranslation\TranslationWrapper':
@@ -705,8 +706,8 @@ services:
     arguments: ['@theme_handler', '@config.factory', '@config.installer', '@module_handler', '@config.manager', '@asset.css.collection_optimizer', '@router.builder', '@logger.channel.default', '@state', '@extension.list.module', '@theme.registry', '@extension.list.theme']
   Drupal\Core\Extension\ThemeInstallerInterface: '@theme_installer'
   entity.memory_cache:
-    class: Drupal\Core\Cache\MemoryCache\MemoryCache
-    arguments: ['@datetime.time']
+    class: Drupal\Core\Cache\MemoryCache\LruMemoryCache
+    arguments: ['@datetime.time', '%entity.memory_cache.slots%']
   Drupal\Core\Cache\MemoryCache\MemoryCacheInterface: '@entity.memory_cache'
   entity_type.manager:
     class: Drupal\Core\Entity\EntityTypeManager
diff --git a/core/modules/migrate/src/MigrateExecutable.php b/core/modules/migrate/src/MigrateExecutable.php
index e00e8af74760..a087ae6875e4 100644
--- a/core/modules/migrate/src/MigrateExecutable.php
+++ b/core/modules/migrate/src/MigrateExecutable.php
@@ -612,9 +612,6 @@ protected function attemptMemoryReclaim() {
     // plenty of memory to continue.
     drupal_static_reset();
 
-    // Entity storage can blow up with caches, so clear it out.
-    \Drupal::service('entity.memory_cache')->deleteAll();
-
     // @todo Explore resetting the container.
 
     // Run garbage collector to further reduce memory.
-- 
GitLab


From a7d041a9a834d7f0f4235e2a7adb6ae3f83a35a4 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Wed, 12 Mar 2025 13:15:43 +0000
Subject: [PATCH 2/2] Add container parameter to default.services.yml

---
 core/assets/scaffold/files/default.services.yml | 5 +++++
 sites/default/default.services.yml              | 5 +++++
 2 files changed, 10 insertions(+)

diff --git a/core/assets/scaffold/files/default.services.yml b/core/assets/scaffold/files/default.services.yml
index 1243d0600898..3570cfa934e6 100644
--- a/core/assets/scaffold/files/default.services.yml
+++ b/core/assets/scaffold/files/default.services.yml
@@ -238,6 +238,11 @@ parameters:
     # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials
     supportsCredentials: false
 
+  # The maximum number of entities stored in memory. Lowering this number can
+  # reduce the amount of memory used in long-running processes like migrations,
+  # however will also increase requests to the database or entity cache backend.
+  entity.memory_cache.slots: 1000
+
   queue.config:
     # The maximum number of seconds to wait if a queue is temporarily suspended.
     # This is not applicable when a queue is suspended but does not specify
diff --git a/sites/default/default.services.yml b/sites/default/default.services.yml
index 1243d0600898..3570cfa934e6 100644
--- a/sites/default/default.services.yml
+++ b/sites/default/default.services.yml
@@ -238,6 +238,11 @@ parameters:
     # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials
     supportsCredentials: false
 
+  # The maximum number of entities stored in memory. Lowering this number can
+  # reduce the amount of memory used in long-running processes like migrations,
+  # however will also increase requests to the database or entity cache backend.
+  entity.memory_cache.slots: 1000
+
   queue.config:
     # The maximum number of seconds to wait if a queue is temporarily suspended.
     # This is not applicable when a queue is suspended but does not specify
-- 
GitLab