The detach() step is not necessary, since clear() does that for all entities currently tracked by Doctrine.
I like to flush() then immediately gc_collect_cycles() every X objects in a loop as that keeps the memory usage somewhat constant. Here's an example:
gc_enable();
foreach ($objects as $i => $obj) {
$em->persist($obj);
// change 500 depending on object size and available memory
if ($i % 500 == 0) {
$em->flush();
gc_collect_cycles();
}
}
// Once more for the remainder.
$em->flush();
gc_collect_cycles();
I would add a few things.
The
detach()
step is not necessary, sinceclear()
does that for all entities currently tracked by Doctrine.I like to
flush()
then immediatelygc_collect_cycles()
every X objects in a loop as that keeps the memory usage somewhat constant. Here's an example: