views:

134

answers:

4

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.

Any ideas on what I can do to fix this? I'd be happy to provide more info on the problem if you need it.

Regards Nicklas


Well, I've stopped using Symfony and started using CakePHP instead which feels much more lightweight and is actually easier to use.

A: 

Hi, 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
addWhere and andWhere are synonyms of each other and work the same, your changes made no difference on the memory leak.
Nicklas Ansman
Cool, didnt know that about add/andWhere, sorry I couldnt be of any help.
Luke
No problem, thanks for trying :)
Nicklas Ansman
A: 

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
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
Sorry, should be Doctrine_Manager::connection()->clear();
Intru
It made no difference :(
Nicklas Ansman
+3  A: 

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

// ...
$data->free(true) ;
unset($data) ;
// ...
Romain Deveaud
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