How do I free the memory used from node_load()? I am using code similar to the following one.

for ($i = 0; $i &lt; 1000000; $i++) {
   $nid = get_node_id($i);
   $node = node_load($nid);
   $data = [
     $node-&gt;field_1,
     $node-&gt;field_3,
     ...
   ];
   save_to_database($data);

   echo round(memory_get_usage() / 1024 / 1024, 2) . ' MB'. PHP_EOL;
}


Every time a loop is completed, the used memory is increased. For example, after the first loop, the used memory is 110 MB, and after the third loop is 230 MB.

How do I avoid the used memory keep increasing?
It happens because node_load(), as every Drupal function that loads entities from the database, stores in memory the loaded entity. If you want to avoid that happens, you can use code similar to the following one.

for ($i = 0; $i &lt; 1000000; $i++) {
  $nid = get_node_id($i);
  $node = entity_load('node', array($nid), array(), TRUE);
  $data = [
    $node-&gt;field_1,
    $node-&gt;field_3,
  ];
  save_to_database($data);
}


The last argument of entity_load() is telling Drupal not to store in memory cache the previously loaded entity.

This is what happens with the default entity controller implemented from Drupal, DrupalDefaultEntityController, which uses the following code for DrupalDefaultEntityController::resetCache().

public function resetCache(array $ids = NULL) {
  if (isset($ids)) {
    foreach ($ids as $id) {
      unset($this-&gt;entityCache[$id]);
    }
  }
  else {
    $this-&gt;entityCache = array();
  }
}