问题:
在运行使用以下 Factory 类中的 Doctrine 的守护程序服务时,存在内存问题。当守护程序服务启动时,它运行大约 175MB。一天后它大约是 250MB,再过一天它是 400MB。我正在寻找导致内存增加的原因以及如何降低内存。
我尝试过的事情:
- $em->clear(); // 这有点帮助
- $em->close(); // 这会导致问题
$em->getConnection()->getConfiguration()->setSQLLogger(null);
--env=prod 应该处理 setSQLLogger(null),对吗?
我应该做些什么来帮助使用 Doctrine 2.x 和 Symfony 2.1.x 解决内存问题?
创建了一个工厂来处理连接
===================== 启动 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 =====================
我使用构造 EMFactory 的抽象类
===================== 启动抽象类 =====================
/**
* @param \Symfony\Component\DependencyInjection\Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
$this->entityManagerFactory = new EMFactory($container);
}
===================== END抽象类=====================
这是我如何使用 EM 的示例,该类扩展了上面的 Abstract 类
===================== 开始工作示例#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 工作示例#1 =====================
这是我如何使用 EM 的另一个示例,该类扩展了上面的 Abstract 类
===================== 开始工作示例#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 工作示例#2 =====================
这是我如何使用 EM 的另一个示例,该类扩展了上面的 Abstract 类
===================== 开始工作示例#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 工作示例#3 =====================
这些只是一些基本示例,但只是查询变得更加复杂。
有什么突出的东西说,优化我?