Visited collections are cleared only in commit(). Commit clears up only if it actually has something to do. Processing large amounts of records without changing them cause visitedCollections to grow without any way of clearing.
could you add a test for this ?
@stof can't really test this one since there's no way to access the visited collections...
This explains why some people have memory leaks even when using clear.
@shulcsm have you tested that this is actually the case in visitedCollections. I can't come up with a flow diagram of how this state is actually reached. The property is only set, when you call flush on the EntityManager.
Early return point here https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/UnitOfWork.php#L315 cause this.