When should EntityManager::clear() run inside a Doctrine batch loop to stop memory growth without losing pending changes?
I am processing a large result set in a long-running PHP CLI job using Doctrine ORM. Memory usage climbs steadily as the loop iterates, which I understand is the UnitOfWork identity map retaining every managed entity. The documented mitigation is periodic flush() followed by EntityManager::clear() , but I am unsure how to apply it safely in my case. My loop