1

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.

Nicklas Ansman
  • 121
  • 3
  • 10

4 Answers4

5

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

// ...
$data->free(true) ;
unset($data) ;
// ...
Romain Deveaud
  • 824
  • 5
  • 11
  • That actually worked! Weird, PHP should clean that up by itself. Still gonna use CakePHP instead, it feels better :) I can add that I did do unset($q) aswell. – Nicklas Ansman Jun 07 '10 at 14:16
1

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

Intru
  • 650
  • 1
  • 4
  • 16
  • 1
    I'm using 5.3.2. Are you sure Doctrine_Manager::connection()->clean() is correct? I can't find it in the API and I can't call it. – Nicklas Ansman Jun 05 '10 at 13:48
0

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
Luke
  • 20,878
  • 35
  • 119
  • 178
0

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;
Luke
  • 3,333
  • 2
  • 28
  • 44