2013-01-31 31 views
3

我正在使用symfony 1.4 + propel 1.6,我想将所有用户数据库导出(索引)到ElasticSearch。PHP sf.1.4 Propel 1.6:循环结果时内存泄漏

我写了所有的脚本,一切都运行正常,除了一个问题。我做了一个循环,重复大约20.000次,每次memory_usage增加。

问题是:它不应该,因为我毁了所有的引用。

我认为Propel正在离开某个地方对我创建的每个对象进行静态引用。虽然找不到它,因为我已经禁用了实例池。

有人曾经有类似的问题吗?也许有人有一个想法如何调试PHP内存限制? (webgrind doesnt)我在这段代码调试上花了几个小时,仍然无法修复它。

// 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()); 

功能$实例 - > toElastic是某事像那:

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; 
} 
+1

你试过'$ instance-> clearAllReferences(true); $ instance = null; $ _document = null;'未设置之前? – j0k

+0

@ j0k这一行代码马上解决了我的问题!非常感谢,我早上应该知道:< – jtompl

+0

哦,太棒了,所以我发表了我的评论作为回答:) – j0k

回答

5

你有你重置尝试过这个?

// 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; 

您可以从this answer获取更多提示。看看这3个链接,真的很有趣,即使其中一个是法文的。