问题:
运行使用以下工厂类的Doctrine守护进程服务时,存在内存问题。当守护进程服务启动时,它占用约175MB的内存。一天后,它增加到大约250MB,再过一天就会达到400MB。我正在查找是什么原因导致内存增加以及如何降低它。
已尝试的事项:
创建了一个工厂来处理连接
===================== 开始 EMFactory =====================
运行使用以下工厂类的Doctrine守护进程服务时,存在内存问题。当守护进程服务启动时,它占用约175MB的内存。一天后,它增加到大约250MB,再过一天就会达到400MB。我正在查找是什么原因导致内存增加以及如何降低它。
已尝试的事项:
- $em->clear(); // 这有点帮助
- $em->close(); // 这会导致问题
$em->getConnection()->getConfiguration()->setSQLLogger(null);
--env=prod 应该正确处理setSQLLogger(null)吗?
创建了一个工厂来处理连接
===================== 开始 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
}
}
}
我将使用一个抽象类来构建EMFactory
===================== 抽象类开始 =====================
/**
* @param \Symfony\Component\DependencyInjection\Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
$this->entityManagerFactory = new EMFactory($container);
}
===================== 抽象类结束 =====================
这里有一个我如何使用EM的例子,该类继承了上面的抽象类
===================== 开始工作示例 #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);
===================== 工作示例 #1 结束 =====================
以下是另一个我如何使用 EM 的示例。该类扩展了上面的抽象类。
===================== 工作示例 #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;
===================== 结束工作示例 #2 =====================
这是我使用 EM 的另一个示例,该类继承了上面的抽象类。
===================== 开始工作示例 #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);
===================== 结束 工作示例#3 =====================
这些只是一些基本的例子,而查询可能会变得更为复杂。
是否有任何显著的地方表明需要优化?