dongzhan3937 2013-03-22 14:09
浏览 105
已采纳

Doctrine ORM Memory问题

The Problem:

While running a Daemon service that uses Doctrine from the Factory classes below there is a memory issue. When the Daemon Service starts it runs about 175MB. A day later it's about 250MB, one more day and it's at 400MB. I'm looking as to what is causing the increase in memory and how I could bring it down.

Things I've tried:

  • $em->clear(); // This kinda helps
  • $em->close(); // this causes issues
  • $em->getConnection()->getConfiguration()->setSQLLogger(null);

    --env=prod should take care of setSQLLogger(null), correct?

Is there anything I should be doing to help with memory issues using Doctrine 2.x and Symfony 2.1.x?

Created a factory to handle connections

===================== START EMFactory =====================

<?php

namespace NS\Bundle\EMBundle;

use Doctrine\ORM\EntityManager;

class EMFactory
{
    /**
     * @var
     */
    private $container;

    /**
     * @param $container
     */
    public function __construct($container)
    {
        $this->container = $container;
    }

    /**
     * @return EntityManager
     */
    public function getBlahEntityManager()
    {
        return $this->getContainer()->get('doctrine.orm.blah_manager_entity_manager');
    }

    /**
     * @return EntityManager
     */
    public function getFooEntityManager()
    {
        return $this->getContainer()->get('doctrine.orm.foo_manager_entity_manager');
    }

    /**
     * @return EntityManager
     */
    public function getBarEntityManager()
    {
        return $this->getContainer()->get('doctrine.orm.bar_manager_entity_manager');
    }

    /**
     * @return mixed
     */
    public function getContainer()
    {
        return $this->container;
    }

    /**
     * @param $container
     * @return $this
     */
    public function setContainer($container)
    {
        $this->container = $container;
        return $this;
    }

    public function closeEntityManager(EntityManager $em)
    {
        try {
            $em->clear(); // This kinda helps
            //$em->close(); // this causes issues
            //$em->getConnection()->getConfiguration()->setSQLLogger(null); // --env=prod should take care of this
        } catch (\Exception $e) {
            // exception here
        }
    }
}

===================== END EMFactory =====================

I use an Abstract Class that constructs the EMFactory

===================== Start Abstract Class =====================

/**
 * @param \Symfony\Component\DependencyInjection\Container $container
 */
public function __construct(Container $container)
{
    $this->container = $container;
    $this->entityManagerFactory = new EMFactory($container);
}

===================== END Abstract Class =====================

Here is an example of how I'm using the EM, The class extends the Abstract class above

===================== START Working Example #1 =====================

// calling like this looks to be working as expected

$fooEM = $this->getEntityManagerFactory()->getFooEntityManager();

$barResults = $fooEM->getRepository('NS\Bundle\EMBundle\Entity\Bar')->findOneBy(array('id' => 1));

if (!is_object($barResults)) {
    throw new \Exception("Bar is a non object.");
}

// some logic here ...

$this->getEntityManagerFactory()->closeEntityManager($fooEM);

===================== END Working Example #1 =====================

Here is another example of how I'm using the EM, The class extends the Abstract class above

===================== START Working Example #2 =====================

// calling from functions like this

$fooEM = $this->getEntityManagerFactory()->getFooEntityManager();

$dql = 'SELECT b.*
        FROM NS\Bundle\EMBundle\Entity\Bar b            
        WHERE b.id = :id';

$query = $fooEM->createQuery($dql);
$query->setParameter('id', 1);

$barResults = $query->getResult();

$this->getEntityManagerFactory()->closeEntityManager($fooEM);

return $barResults;

===================== END Working Example #2 =====================

Here is another example of how I'm using the EM, The class extends the Abstract class above

===================== START Working Example #3 =====================

// calling from functions like this

$fooEM = $this->getEntityManagerFactory()->getFooEntityManager();

$barEntity = new Bar();
$barEntity->setId(1);
$barEntity->setComment('this is foo-ie');

$fooEM->persist($barEntity);
$fooEM->flush();

$this->getEntityManagerFactory()->closeEntityManager($fooEM);

unset($barEntity);

===================== END Working Example #3 =====================

These are just some basic examples but it's just the queries that get more complex.

Does anything stand out that say, Optimize me?

  • 写回答

2条回答 默认 最新

  • dongxing2692 2013-05-08 12:39
    关注

    This solved the connection issue we were having.

    Needed to close just the connection

    public function closeEntityManager(EntityManager $em)
    {
        try {
            $em->clear(); // This kinda helps
            $em->getConnection()->close(); // this seems to work            
        } catch (\Exception $e) {
            // exception here
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ros的rviz仿真机器人
  • ¥15 关于#linux#的问题(输入输出错误):出现这个界面接着我重新装系统,又让修电脑的师傅帮我扫描硬盘(没有问题)用着用着又卡死(相关搜索:固态硬盘)
  • ¥15 cv::resize不同线程时间不同
  • ¥15 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来