Nicklas Ansman
Nicklas Ansman

Reputation: 121

Memory leak in PHP with Symfony + Doctrine

I'm using PHP with the Symfony framework (with Doctrine as my ORM) to build a spider that crawls some sites.

My problem is that the following code generates a memory leak:

$q = $this -> createQuery('Product p');

if($store) {
    $q
        -> andWhere('p.store_id = ?', $store -> getId())
        -> limit(1);
}

$q -> andWhere('p.name = ?', $name);

$data = $q -> execute();
$q -> free(true);
$data -> free(true);
return NULL;

This code is placed in a subclass of Doctrine_Table. If I comment out the execute part (and of course the $data -> free(true)) the leak stops. This has led me to the conclusion that it's the Doctrine_Collection that's causing the leak.

Upvotes: 1

Views: 4053

Answers (4)

Luke
Luke

Reputation: 21236

I'm just running into the same problem with a CLI command that runs continuously. After spending many hours to troubleshoot this issue it turns out that running my commands in prod mode actually solved the issue:

app/console my:custom:command --env=prod

Upvotes: 0

Romain Deveaud
Romain Deveaud

Reputation: 824

I solved my problems with Doctrine memory leaks by freeing and unseting data, did you try it?

// ...
$data->free(true) ;
unset($data) ;
// ...

Upvotes: 5

Intru
Intru

Reputation: 650

What version of PHP are you using? If it's < 5.3 it probably has to do with the 'recursive references leak memory' bug.

You can also try to call Doctrine_Manager::connection()->clear(), which should clean up the connection and remove identity map entries

Upvotes: 1

Luke
Luke

Reputation: 3353

Should you be using addWhere instead of andWhere? Also I believe a limit should be added at the end of a statement, try:

$q = $this -> createQuery('Product p') -> where('p.name = ?', $name);

 if($store) {
$q
    -> addWhere('p.store_id = ?', $store -> getId())
    -> limit(1);
 }

 $data = $q -> execute();
 $q -> free(true);
 $data -> free(true);
return NULL;

Upvotes: 0

Related Questions