简体   繁体   English

symfony任务中的内存泄漏

[英]Memory leak in symfony task

I have issues with symfony 1.4 a task running daily. 我有symfony 1.4的问题,该任务每天运行。 To make simple the task download an xml then read it. 为了使任务简单,请下载一个xml,然后阅读。

I have notice that the memory used increase between each "node" in the xml. 我注意到xml中每个“节点”之间使用的内存增加了。

For instance, somewhere in the script I do that: 例如,在脚本中的某处我这样做:

 foreach($prd->ArtistList->Artist as $art)
          {
  echo "start foreach artist  ", memory_get_peak_usage(1), " bytes of ram.\n";
            if(!$artist = ArtistTable::getInstance()->findOneByRoleAndName($art['role'], $art['name']))
            {
              $artist = new Artist();
              $artist->role = $art['role'];
              $artist->name = $art['name'];
              $artist->save();
            }

            if(!$pha = ProductArtistTable::getInstance()->findOneByProductIdAndArtistId($prd_id, $artist->id))
            {
              $pha = new ProductArtist();
              $pha->product_id = $prd_id;
              $pha->artist_id = $artist->id;
              $pha->save();
            }

            $pha->free(true);
            $artist->free(true);
            unset($pha);
            unset($artist);

            echo "end foreach artist  ", memory_get_peak_usage(1), " bytes of ram.\n";
          }
          unset($art);

I can see that between two memory_get_peak_usage(1) end and start, memory used increase... I don't know what else am I supose to do to keep it at a same level... 我可以看到在两个memory_get_peak_usage(1)结束和开始之间,已使用的内存增加了...我不知道我还想做什么来将其保持在同一水平...

Do you have any idea how to solve that? 您知道如何解决吗?

Thanks! 谢谢!

I've monitored slow performances in the past within a symfony application. 过去,我在symfony应用程序中监视过缓慢的性能。 Found out that those built in ORM's (Doctrine in my case) query methods are really a bad thing, especially within a foreach loop. 发现那些内置在ORM(在我的例子中为Doctrine)查询方法中的确是一件坏事,尤其是在foreach循环中。

I'd suggest you to try using RAW SQL instead. 我建议您尝试使用RAW SQL代替。

If you don't know how to use raw sql within symfony this should do the trick 如果您不知道如何在symfony中使用原始sql,应该可以解决问题

$connection = Doctrine_Manager::connection();
$statement = $connection->prepare('your sql statement');
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_NUM); // or whatever fetch mode you want to use

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM