Score:3

Drush script memory problem

us flag

I'm running a long drush script and its causing memory problems.

Then I tried debbuging it and I have this very simple code:

$query = \Drupal::entityTypeManager()->getStorage('user');

foreach ($emails as $i => $email) {
    $users = $query->loadByProperties(['mail' => $email]);
    $user = reset($users);

    unset($user);
    unset($users);
}

When I ran the script I noticed that the memory increases dramatically as the script is running. What is causing this memory issue?

leymannx avatar
ne flag
Check the code of https://www.drupal.org/project/drush9_batch_processing
leymannx avatar
ne flag
https://git.drupalcode.org/project/drush9_batch_processing
us flag
@leymannx This doesn't help me, I already have a drush script, I don't need a module, I'm just wondering why was it using a lot memory.
leymannx avatar
ne flag
It's an example module which explains how to implement a batch process in a Drush command. A batch process is what you need to process the users in small chunks and in between these chunks release memory again.
Score:5
cn flag

The memory issue is caused by the internal entity cache which you can release by using $storage->resetCache().

You've named the user storage $query, so replace the unset() commands with:

$query->resetCache([$user->id()]);
$query->resetCache(array_keys($users));

Recently core added the advice to use the entity.memory_cache service instead, to only clear the in-memory cache and not invalidate the entities in the database:

\Drupal::service('entity.memory_cache')->deleteAll();

This is more efficient, in your script and also for subsequent loads of the entities.

Reference: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21ContentEntityStorageBase.php/function/ContentEntityStorageBase%3A%3AresetCache/9.2.x

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.