ORC memory problems ORK
Problem:
There is a memory issue when starting the Daemon service using Doctrine from the below Factory classes. When the Daemon service starts, it starts at about 175MB. In a day it's about 250 MB, another day and 400 MB. I am looking at what is causing the increase in memory and how I can bring it down.
Things I've tried:
- $ em-> clear (); // This kind helps
- $ em-> close (); // this is causing problems.
-
$ em-> GetConnection () -> getConfiguration () -> setSQLLogger (null);
- env = prod should take care of setSQLLogger (null), right?
Should I be doing something to help with memory issues with 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 am using abstract class which creates EMFactory
====================== Beginning of abstract class =====================
/**
* @param \Symfony\Component\DependencyInjection\Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
$this->entityManagerFactory = new EMFactory($container);
}
======================= END Annotation Class =====================
Here is an example of how I am using EM, the class extends the Abstract class above
================================================== ================================================== ================================================== ================================================== ======
// 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 am using EM, the class extends the Abstract class above
====================== Working 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 am using EM, the class extends the Abstract class above
================================================== ================================================== ================================================== ================================================== ======
// 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 these are just complex queries.
Is there something great that tell me to optimize me?
source to share
Your problem may arise in your managers' initiatives. If you have a specific set of these, you can rather use Symfony2 Dependency Injection instead of calling the container.
Every time you use your accessory you are creating a new Entity Manager, hence consuming more memory (and like its daemon, you never release it). Using DI, you will always have the same instance.
Your EMFFactory should look like this:
<?php
namespace NS\Bundle\EMBundle;
use Doctrine\ORM\EntityManager;
class EMFactory
{
/**
* @var
*/
private $fooEm;
/**
* @var
*/
private $barEm;
/**
* @var
*/
private $blahEm;
/**
* @param $fooEm
* @param $barEm
* @param $blahEm
*/
public function __construct($fooEm, $barEm, $blahEm)
{
$this->fooEm = $fooEm;
$this->barEm = $barEm;
$this->blahEm = $blahEm;
}
/**
* @return EntityManager
*/
public function getBlahEntityManager()
{
return $this->blahEm;
}
/**
* @return EntityManager
*/
public function getFooEntityManager()
{
return $this->fooEm;
}
/**
* @return EntityManager
*/
public function getBarEntityManager()
{
return $this->barEm;
}
/**
* @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
}
}
}
Then modify your service definitions to provide different EMs in your configuration and define EMFactory as a service.
source to share