I think you're looking for an Iterator.
This article will show you how to handle big datas.
$q = $this->_em->createQuery("<DQL to select the objects I want>");
$iterableResult = $q->iterate();
while (($row = $iterableResult->next()) !== false) {
// do stuff with the data in the row, $row[0] is always the object
$this->_em->detach($row[0]); // detach from Doctrine, so that it can be GC'd immediately
}
This code, instead of loading all the datas in a single array, creates an iterator to prevent that big loading and this kind of error.
However, if you're just looking on how to increase the memory size of PHP take a look at this answer
ini_set('memory_limit', '1G');