title: How to fix Laravel Excel Reader memory leak?
body: <p>I'm using Laravel Excel <code>&quot;maatwebsite/excel&quot;: &quot;^3.1&quot;,</code> to import just 400 records with 10 fields. The process of just reading these records needs around 2GB of RAM (just to read data into a collection) reading them in chunks 20 rows per once (I tried increasing chunk size to 50 and it consumed more RAM)</p>
  <pre><code>try {
       $collection = Excel::toCollection(
           new UsersImport(
               $entity,
               $user,
               $unique_fields,
               $update_duplicates,
               new JobHistory,
               $this-&gt;userTypeRepository,
           ),
           storage_path().'/uploads/' . $usersFileName
       );
     } catch (\Throwable $th) {
        throw new Exception($th);
  }
  </code></pre>
  <p>UsersImport.php</p>
  <pre><code>class UsersImport implements ToCollection, WithHeadingRow, WithChunkReading, ShouldQueue, WithEvents
  {
  private $entity, $user, $updateDuplicates, $uniqueFields, $jobHistory, $userTypeRepository;
  use RemembersChunkOffset;
  
  public function __construct(Entity $entity, User $user, array $uniqueFields, string $updateDuplicates, JobHistory $jobHistory, UserTypeRepository $userTypeRepository)
  {
      $this-&gt;entity = $entity;
      $this-&gt;user = $user;
      $this-&gt;updateDuplicates = $updateDuplicates;
      $this-&gt;uniqueFields = $uniqueFields;
      $this-&gt;jobHistory = $jobHistory;
      $this-&gt;userTypeRepository = $userTypeRepository;
  }
  
  public function collection(Collection $rows) {
  
      /* foreach ($rows as $row) {
          $data = $row-&gt;toArray();
          array_walk($data, array($this, 'parseData'));
      } */
  }
  
  public function chunkSize(): int
  {
      return 20;
  }
  
  public function registerEvents(): array
  {
      return [];
  }
  
  public function headingRow(): int
  {
      return 15;
  }
  
  }
  </code></pre>
  <p>Why is this happening and how to make it consume less RAM? If I try to import 800 users I'll need 4GB RAM to make it work!!</p>