在循环中执行Doctrine查询时出现内存泄漏 [英] Memory leak when executing Doctrine query in loop

查看:82
本文介绍了在循环中执行Doctrine查询时出现内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的脚本中找到内存泄漏的原因我遇到麻烦。我有一个简单的存储库方法,它将实体中的count列增加X个数量:

  public function incrementCount ,$ amount)
{
$ query = $ this
- > createQueryBuilder('e')
- > update('MyEntity','e')
- > set('e.count','e.count +:amount')
- > where('e.id =:id')
- > setParameter(' id',$ id)
- > setParameter('amount',$ amount)
- > getQuery();

$ query-> execute();
}

问题是,如果我在循环中调用这个内存使用气球迭代:

  $ doctrineManager = $ this-> getContainer() - > get('doctrine') - > getManager (); 
$ myRepository = $ doctrineManager-> getRepository('MyEntity');
while(true){
$ myRepository-> incrementCount(123,5);
$ doctrineManager-> clear();
gc_collect_cycles();
}

我在这里缺少什么?我试过 - > clear(),根据Doctrine的关于批处理的建议。我甚至试过 gc_collect_cycles(),但仍然存在问题。



我正在运行Doctrine 2.4.6 on PHP 5.5。

解决方案

我通过添加 - no-debug 我的命令事实证明,在调试模式下,分析器正在存储有关内存中每个单个查询的信息。


I'm having trouble in locating the cause for a memory leak in my script. I have a simple repository method which increments a 'count' column in my entity by X amount:

public function incrementCount($id, $amount)
{
    $query = $this
        ->createQueryBuilder('e')
        ->update('MyEntity', 'e')
        ->set('e.count', 'e.count + :amount')
        ->where('e.id = :id')
        ->setParameter('id', $id)
        ->setParameter('amount', $amount)
        ->getQuery();

    $query->execute();
}

Problem is, if I call this in a loop the memory usage balloons on every iteration:

$doctrineManager = $this->getContainer()->get('doctrine')->getManager();
$myRepository = $doctrineManager->getRepository('MyEntity');
while (true) {
    $myRepository->incrementCount("123", 5);
    $doctrineManager->clear();
    gc_collect_cycles();
}

What am I missing here? I've tried ->clear(), as per Doctrine's advice on batch processing. I even tried gc_collect_cycles(), but still the issue remains.

I'm running Doctrine 2.4.6 on PHP 5.5.

解决方案

I resolved this by adding --no-debug to my command. It turns out that in debug mode, the profiler was storing information about every single query in memory.

这篇关于在循环中执行Doctrine查询时出现内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆