3

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.

5
  • What if you also call unset($query)? Commented Mar 6, 2013 at 12:12
  • Just added unset($query) next to the other unset : $this->clear(true); $query->getQuery()->free(true); unset($query); unset($r); but it's the same : mem 4 : 77.849983215332 Commented Mar 6, 2013 at 12:27
  • And what are the results? Commented Mar 6, 2013 at 12:29
  • the same : mem 4 : 77.849983215332 Commented Mar 6, 2013 at 12:35
  • I think it has something to do with sqllogger enabled in debug mode stackoverflow.com/a/10913115/842075 Commented Mar 6, 2013 at 14:03

1 Answer 1

5

Doctrine caches certain aspects of Entities it has loaded. Use

$em->clear(); 

to detach all objects from the current entity manager.

Sign up to request clarification or add additional context in comments.

2 Comments

With this, the memory is down to 66 mb (mem 4).
The additional memory is the result of the $r variable. unset() does not guarantee immediate garbage collection, so the process will retain that memory until the garbage collector has freed it or the process has terminated. See stackoverflow.com/questions/584960/… for more detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.