Problem/Motivation
On a long-running process, such as one that load/saves thousands of entities, DriverBase::statsWrite grows without limit and eventually causes a Fatal error: Allowed memory size of … bytes exhausted error.
This happens because DriverBase::statsWrite records every operation with no mechanism to clear/reset the self::$stats array or disable statistics tracking entirely.
It's worth noting that these values appear to only be needed/used when the memcache_admin module is enabled—yet the stats are written even when that module is disabled.
Steps to reproduce
n/a
Proposed resolution
Only enable stats collection when memcache_admin is installed and configured to display them - reimplementing the way things work in Drupal 7 but in modern Drupal fashion.
Remaining tasks
User interface changes
None
API changes
New container parameter memcache.stats_collect that defaults to FALSE. If the memcache admin config show_memcache_statistics is set to true then the container parameter will be updated.
New method \Drupal\memcache\Driver\DriverBase::disableStats that can be called to disable stats collection. This is used to disable stat collection when a user does not have the permission.
The container parameter is injected into \Drupal\memcache\Driver\DriverBase::__construct() by \Drupal\memcache\Driver\MemcacheDriverFactory which has the new container parameter injected by memcache.services.yml
Data model changes
None
Issue fork memcache-3503083
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
gabesulliceAfter doing some more research, I believe there used to be functionality to disable stat tracking, but it was commented out when the module was ported to Drupal 8.
https://git.drupalcode.org/project/memcache/-/blame/8.x-2.x/src/Driver/D...
Comment #3
alexpott@gabesullice lolz I was just about to report this. Imo stat collection should be off by default and only on when configured with a container parameter. Otherwise the default mode of a module meant for performance is to be a memory hog.
Comment #4
alexpottThe amusing this is I realised this yesterday and was going to create an issue next week when I had $time to do this. The bug has been sitting here for years and @gabesullice realised it on the same day in 2025!
Comment #6
alexpottThis is a major bug. Memcache's memory usage is much higher than it should be because of this.
I've started to fix this in the MR. Just need to implement the checking if the account has access.
Comment #7
alexpottComment #8
mably commentedGetting this error, even after an
update.php, when applying the patch on my Drupal 11 instance:Comment #9
alexpottComment #10
mably commentedTested locally, seems to be working fine 👍