Memory leak when executing Doctrine query in loop

后端 未结 6 1768
梦毁少年i
梦毁少年i 2020-12-31 07:32

I\'m having trouble in locating the cause for a memory leak in my script. I have a simple repository method which increments a \'count\' column in my entity by X amount:

6条回答
  •  生来不讨喜
    2020-12-31 07:39

    For me it was clearing doctrine, or as the documentation says, detaching all entities:

    $this->em->clear(); //Here em is the entity manager.
    

    So inside my loop y flush every 1000 iterations and detach all entities (I don't need them anymore):

        foreach ($reader->getRecords() as $position => $value) {
            $this->processValue($value, $position);
            if($position % 1000 === 0){
                $this->em->flush();
                $this->em->clear();
            }
            $this->progress->advance();
        }
    

    Hope this helps.

    PS: here's the documentation.

提交回复
热议问题