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

Command icon 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

gabesullice created an issue. See original summary.

gabesullice’s picture

After 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...

alexpott’s picture

@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.

alexpott’s picture

The 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!

alexpott’s picture

Priority: Normal » Major
Status: Active » Needs review

This 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.

alexpott’s picture

Issue summary: View changes
mably’s picture

Getting this error, even after an update.php, when applying the patch on my Drupal 11 instance:

Drupal\\Core\\DependencyInjection\\ContainerNotInitializedException: \\Drupal::$container is not initialized yet. \\Drupal::setContainer() must be called with a real container. in /var/www/html/web/core/lib/Drupal.php on line 169 #0 /var/www/html/web/modules/contrib/memcache/src/Driver/DriverBase.php(305): Drupal::getContainer()\n#1 /var/www/html/web/modules/contrib/memcache/src/Driver/DriverBase.php(102)
alexpott’s picture

Issue summary: View changes
mably’s picture

Tested locally, seems to be working fine 👍