I am working with symfony 1.4 + propel 1.6 and I want to export (index) all my user database to ElasticSearch.
I have written all the script and everything is working okay, besides one problem. I make a loop that repeats about 20.000~ times and with everytime the memory_usage increases.
Problem is: it shouldn't, because I am destroying all the references.
I think Propel is leaving somewhere a static reference to the every object I create. Can't find it though, because I already disabled the instance pooling.
Anybody had ever a similar problem? Maybe someone has an idea how can I debug the PHP memory limits? (webgrind doesnt) I spent last few hours on this piece of code debugging and still can't fix it.
// optimizations
gc_enable();
Propel::getConnection()->useDebug(false);
Propel::disableInstancePooling();
// the while
$offset = 0;
$perpage = 10;
$c = SearchUserQuery::create()->limit($perpage);
do {
$rs = SearchUserPeer::doSelectStmt($c);
while ($row = $rs->fetch(PDO::FETCH_NUM))
{
$instance = new SearchUser();
$instance->hydrate($row);
$data = $instance->toElastic(); // this line makes a lot of memory leak
$_document = new Elastica\Document($instance->getPrimaryKey(), $data);
$_type->addDocument($_document);
unset($_document, $instance);
}
$c->offset($offset += $perpage);
} while( $rs->rowCount() );
Function $instance->toElastic is sth like that:
public function toElastic()
{
return Array(
'profile' => $this->toArray(BasePeer::TYPE_COLNAME, false),
'info' => $this->getUserInfo()->toArray(BasePeer::TYPE_COLNAME, false),
'branches' => $this->getElasticBranches(),
);
}
/**
* @return array(id,name)
*/
public function getElasticBranches()
{
$branches = Array();
foreach ($this->getsfGuardUser()->getUserBranchs() as $branch)
$branches[] = Array(
'id' => $branch->getBranchId(),
'name' => $branch->getName()
);
return $branches;
}
Have you tried this before unsetting?
// garbage collector problem in PHP 5.3
$instance->clearAllReferences(true);
// remove the variable content before removing the address (with unset)
$instance = null;
$_document = null;
$_type = null;
You can grab more tips from this answer. Looks at the 3 links, there are really interesting, even if one of them is in french.