原则ORM内存问题 [英] Doctrine ORM Memory issues

查看:95
本文介绍了原则ORM内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:



在下面的Factory类中运行使用Doctrine的Daemon服务时,会出现内存问题。 Daemon服务启动时运行大约175MB。一天后,它大约是250MB,一天多了400MB。我正在寻找引起内存增加的原因,以及如何将其降低。



我试过的事情:




  • $ em-> clear(); //这样有助于

  • $ em-> close(); //这会导致问题

  • $ em-> getConnection() - > getConfiguration() - > setSQLLogger(null);



    - env = prod应该照顾setSQLLogger(null),是否正确?




有没有什么我应该使用Doctrine 2.x和Symfony 2.1.x帮助记忆问题?



创建一个工厂处理连接



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

 <?php 

命名空间NS \\Bundle\EMBundle;

请使用Doctrine\ORM\EntityManager;

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

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

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

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

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

/ **
* @return mixed
* /
public function getContainer()
{
return $这 - >容器;
}

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

public function closeEntityManager(EntityManager $ em)
{
try {
$ em-> clear(); //这样有助于
// $ em-> close(); //这会导致问题
// $ em-> getConnection() - > getConfiguration() - > setSQLLogger(null); // --env = prod应该照顾这个
} catch(\Exception $ e){
// exception here
}
}
}

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



我使用构造EMFactory的抽象类



=====================开始抽象类=============== ======

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

============== ===== END抽象类===================



这是一个例子我正在使用EM,该类扩展了上面的Abstract类



===================开始工作示例#1 ===================

  //这样的调用看起来像预期一样工作

$ 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);
}

//这里有一些逻辑...

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

=================== END工作示例#1 =====================



这是另一个例子,我如何使用EM,该类扩展了上面的Abstract class



===================== START工作示例#2 ===================

  //调用从这样的函数

$ 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工作示例#2 ===================



这是另一个例子,使用EM,该类扩展了上面的Abstract class



===================== START工作示例#3 ===================

  // call从这样的功能

$ 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工作示例#3 ===================



这些只是一些基本的例子,但它只是这些查询变得更加复杂。



有什么可以说是优化我吗?

解决方案

这解决了我们遇到的连接问题。



需要关闭只是连接

  public function closeEntityManager(EntityManager $ em)
{
try {
$ em-> clear(); //这样有助于
$ em-> getConnection() - > close(); //这似乎工作
} catch(\Exception $ e){
// exception here
}
}
/ pre>

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?

解决方案

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
    }
}

这篇关于原则ORM内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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