Symfony's job is memory leak
I wrote a symfony task to populate a sampled database. Here's a sample code snippet:
gc_enable();
Propel::disableInstancePooling();
public function test()
{
for($i = 0; $i < 10000; $i++) {
$this->doIt($i);
}
}
public function doIt($i)
{
$user = new User();
$user->setUsername('user' . $i . "@example.com");
$user->setPassword('test');
$user->setFirstName('firstname' . $i);
$user->setLastName('surname' . rand(0, 1000));
$user->save();
$user->clearAllReferences(true);
$user = null;
gc_collect_cycles();
}
How can I limit my memory usage?
source to share
You have some good advice in another SO formatted thread .
And here is a really good blog post about memory leaks using propel . It's in French, but it's really interesting.
And if you're working on big data (like bulk imports), you should also take a look at pcntl_fork ( see the point ). pcntl_fork does not work on Windows. I used this method to work with large imports and it is very fast and didn't eat all your memory.
source to share
This is the final code. It can run for some time with the same memory usage. All.
public function test()
{
for($i = 0; $i < 10000; $i++) {
$this->doIt($i);
}
}
public function doIt($i)
{
gc_enable();
Propel::disableInstancePooling();
$user = new User();
$user->setUsername('user' . $i . "@example.com");
$user->setPassword('test');
$user->setFirstName('firstname' . $i);
$user->setLastName('surname' . rand(0, 1000));
$user->save();
$this->delete($user);
}
public function delete($obj)
{
$obj->clearAllReferences(true);
unset($obj);
// redundant
// gc_collect_cycles();
}
source to share
Symfony console tasks require quite a lot of PHP memory, especially on Windows. If the Propel task is not working, I would recommend permanently changing the php.ini file setting with at least 256M memory allocation. I know it sounds high, but you only need these tasks on a development machine.
source to share