Dealing with memory issues on long running scripts in Drupal

Submitted by barnettech on

This method actually worked very nicely today:

$query = \Drupal::entityQuery('node')
      ->condition('type', 'aci_user_data');
    $nids = $query->execute();

    $chunks = array_chunk($nids, 100); // Here you're creating chunks of Node IDs.
    $all_nodes = [];
    foreach ($chunks as $chunk) {
      $nodes = Node::loadMultiple($chunk); // Loading nodes by IDs.
      $all_nodes = $all_nodes + $nodes; // Merging all nodes in one array.

      \Drupal::entityTypeManager()->getStorage('node')->resetCache($chunk);
      \Drupal::entityTypeManager()->getStorage('node')->resetCache($nodes);
      //print "Mem usage: ".memory_get_usage(true)."\n
"; } // Prepare CSV data. $csv_data = []; $csv_data[] = ['Title', 'Record Created Date', 'field_aci_dashboard_array', 'field_aci_drupal_user_id', 'field_aci_results_array', 'field_aci_user_email', 'field_aci_uuid']; // Replace with your field names. $count = 0; foreach ($all_nodes as $node) {

https://drupal.stackexchange.com/questions/280707/why-does-this-code-us…

supposedly this works also:

$memory_cache = \Drupal::service('entity.memory_cache');
$entity_storage = \Drupal::entityTypeManager()->getStorage('node');
$nids = \Drupal::entityQuery('node')
  ->condition('type', 'issues')
  ->execute();
    
$chunks = array_chunk($nids, 100);
    
foreach ($chunks as $chunk) {
  $entities = $entity_storage->loadMultiple($chunk);
    
  foreach ($entities as $entity) {
    // Do your stuff with the node/other entities.
  }
    
  $memory_cache->deleteAll();
}