Seb33300
Seb33300

Reputation: 7566

Symfony 2 Doctrine memory usage

I'm creating a query with an entity repository and it seems to have memory leaks.

In my Entity repository class:

echo 'mem 1 : ' . (memory_get_usage()/1024/1024) . "<br />\n";
$query = $this->createQueryBuilder('a')->select('a','b','c','...');
echo 'mem 2 : ' . (memory_get_usage()/1024/1024) . "<br />\n";
$r = $query->getQuery()->getResult();
echo 'mem 3 : ' . (memory_get_usage()/1024/1024) . "<br />\n";
$this->clear(true);
$query->getQuery()->free(true);
unset($r);
echo 'mem 4 : ' . (memory_get_usage()/1024/1024) . "<br />\n";

Output:

mem 1 : 5.0805282592773
mem 2 : 5.0998611450195
mem 3 : 91.49528503418
mem 4 : 77.939567565918

Why is the memory not back to the initial size (5 MB) after freeing the memory?

And only pass from 91 to 77.

Upvotes: 3

Views: 5376

Answers (1)

james_t
james_t

Reputation: 2733

Doctrine caches certain aspects of Entities it has loaded. Use

$em->clear(); 

to detach all objects from the current entity manager.

Upvotes: 5

Related Questions