PHP sf.1.4 Propel 1.6:循环结果时内存泄漏
作者:互联网
我正在使用symfony 1.4 propel 1.6,我想将所有用户数据库导出(索引)到ElasticSearch.
我写了所有的脚本,除了一个问题外,一切正常.我创建一个重复大约20.000次的循环,每次memory_usage增加.
问题是:它不应该,因为我正在摧毁所有的引用.
我认为Propel正在为我创建的每个对象留下静态引用.但是找不到它,因为我已经禁用了实例池.
有人遇到过类似的问题吗?也许有人知道如何调试PHP内存限制? (webgrind不)我花了最后几个小时在这段代码调试上仍然无法修复它.
// 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() );
函数$instance-> 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;
}
解决方法:
在取消设置之前你试过这个吗?
// 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个链接,真的很有趣,即使其中一个是法语.
标签:symfony-1-4,propel,php,memory-leaks,elastica 来源: https://codeday.me/bug/20190901/1782131.html