Memory exhausted with clear() #7119

Closed
opened 2026-01-22 15:45:07 +01:00 by admin · 0 comments
Owner

Originally created by @duzenko on GitHub (Mar 15, 2023).

Bug Report

Q A
BC Break no
Version 2.8.2

Summary

I wrote a new utility script that I run in CLI. It processes many records.

    foreach( $contractIds as $contractId ) {
        $contract = $entityManager->find( LoanContract::class, $contractId );
        $result = processContract( $contract );
        $processingResult[ $result ]++;
        $entityManager->clear();
    }

Current behavior

It works as expected locally and on staging, but on production it runs out of memory (standard 256MB).
I have spent much time trying to debug it but the only thing that helps seems to be commenting out the clear() call.

    foreach( $contractIds as $contractId ) {
        $contract = $entityManager->find( LoanContract::class, $contractId );
        $result = processContract( $contract );
        $processingResult[ $result ]++;
//      $entityManager->clear();
    }

This version does not exhaust memory but takes much longer to complete.

I would appreciate any insight in what's going on here.

Attached is the log from two invocations: with clear() and without.
image

Originally created by @duzenko on GitHub (Mar 15, 2023). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | no | Version | 2.8.2 #### Summary I wrote a new utility script that I run in CLI. It processes many records. ``` foreach( $contractIds as $contractId ) { $contract = $entityManager->find( LoanContract::class, $contractId ); $result = processContract( $contract ); $processingResult[ $result ]++; $entityManager->clear(); } ``` #### Current behavior It works as expected locally and on staging, but on production it runs out of memory (standard 256MB). I have spent much time trying to debug it but the only thing that helps seems to be commenting out the clear() call. ``` foreach( $contractIds as $contractId ) { $contract = $entityManager->find( LoanContract::class, $contractId ); $result = processContract( $contract ); $processingResult[ $result ]++; // $entityManager->clear(); } ``` This version does not exhaust memory but takes much longer to complete. I would appreciate any insight in what's going on here. Attached is the log from two invocations: with `clear()` and without. ![image](https://user-images.githubusercontent.com/1481807/225399352-a2c77185-34e1-4ba8-aa2b-0e7c3737e44b.png)
admin closed this issue 2026-01-22 15:45:07 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7119