title: How to free the kernel of Symfony (PHP) used in the threads?
body: <p>In php 7.0.2, develop a symfony2 command in which two-level threads (fork) are created.</p>
  
  <p>There is a Container class that instantiates the MyWorker class (extends from Worker) and calls the start () function for each MyWorker element in the $workers array.</p>
  
  <p>With the call to the function start () of MyWorker the function run in which a Symfony instance is created, data is searched in the repositories, 5 children belonging to the MyThread class are created by each MyWorker and is called the function start () that invokes the run function.</p>
  
  <p>In the run function of MyThread, Symfony is instantiated again (I did not find a way to do it through the parent instance).</p>
  
  <pre><code>    class Container
      {
          protected $user; 
  
          public function process()
          {
              $param = array();
  
              // ... Some logic
  
              $workers = array();
              for ( $j = 0; $j &lt; 5; $j++ )
              {
                  $workers[] = new MyWorker( $param );
              }
  
              /**
               * Process the workers
               */
              foreach ( $workers as $work )
              {
                  $work-&gt;setUser($this-&gt;user);
                  $work-&gt;start();
              }       
  
          }   
      }
  </code></pre>
  
  <hr>
  
  <pre><code>    class MyWorker extends \Worker
      {
          protected $container;
          protected $document_manager;    
          protected $id;  
  
          public function __construct ( $param )
          {
              $id = $param['id'];
              //... Set other parameters
          }
  
  
          public function setDoctrineManagerInWorker()
          {
              //Set require Autoload and AppKernel
              require_once __DIR__.'/../../../../app/autoload.php';
              require_once __DIR__.'/../../../../app/AppKernel.php';
  
              //Creating a new AppKernel with the given environment
              $kernel = new \AppKernel( 'dev', 1 );
              //Loading the Cache and Classes
              $kernel-&gt;loadClassCache();
              $kernel-&gt;boot();
  
              //Set container
              $container = $kernel-&gt;getContainer();
  
              //Set document manager
              $document_manager = $kernel-&gt;getContainer()-&gt;get('doctrine')-&gt;getManager();
          }
  
  
          public function run()
          {
              $this-&gt;setDoctrineManagerInWorker();        
  
              //... Aqui se aplica la logica para acceder a los repositorios
  
              $worker = $this-&gt;document_manager-&gt;getRepository('MyBundle:Worker')-&gt;find( $this-&gt;getId() );        
  
              //...
  
              for ( $i = 0; $i &lt; 5; $i++ )
              {
                 $thread = new MyThread( $param );
                 $thread-&gt;start();
                 $thread-&gt;join();
              }
          }
      }
  </code></pre>
  
  <hr>
  
  <pre><code>    class MyThread extends \Thread
      {
          protected $container;
          protected $document_manager;    
  
          public function __construct ( $param )
          {
              //... Set parameters
          }
  
          public function setDoctrineManagerInThread()
          {
              //Set require Autoload and AppKernel
              require_once __DIR__.'/../../../../app/autoload.php';
              require_once __DIR__.'/../../../../app/AppKernel.php';
  
              $kernel = new \AppKernel( 'dev', 1 );
              //Loading the Cache and Classes
              $kernel-&gt;loadClassCache();
              $kernel-&gt;boot();
              //Set container
              $this-&gt;container = $kernel-&gt;getContainer();
              //Set document manager
              $this-&gt;document_manager= $kernel-&gt;getContainer()-&gt;get('doctrine_mongodb')-&gt;getManager();
          }
  
          public function run()
          {
              $this-&gt;setDoctrineManagerInThread();
  
              //... Logical processing of repositories
              $thread = $this-&gt;document_manager-&gt;getRepository('MyBundle:Thread')-&gt;findAll();
              //...
          }
      }
  </code></pre>
  
  <p>The problem is that when the process () function is called repeatedly (more than 200 times), the following memory error is generated:</p>
  
  <pre><code>mmap () failed: [12] Can not allocate memory. Fatal error: Out of memory (allocated 2820669440) (tried to allocate 32768 bytes) in /srv/www/leadpro.com/current/vendor/symfony/symfony/src/Symfony/Component/Debug/Exception/OutOfMemoryException.php on line 1
  </code></pre>
  
  <p>I'm assuming that the error must be generated because symfony instances are not released within the Workers and Threads.</p>
  
  <p>Please, if anyone can suggest a solution, I would be very grateful.</p>