1

I wrote a symfony task to fill a database of sample data. Here's a sample piece of code:

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 the use of memory?

hakre
  • 193,403
  • 52
  • 435
  • 836
Piotr Kowalczuk
  • 400
  • 5
  • 19
  • possible duplicate http://stackoverflow.com/questions/2097744/php-symfony-doctrine-memory-leak – j0k Jul 05 '12 at 07:14

3 Answers3

3

This is final code. It could work inf amount of time at same memory usage level. Thx everybody.

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();
}
Piotr Kowalczuk
  • 400
  • 5
  • 19
  • Thanks for this, it works for me too. However, I noticed that gc_collect_cycles(); is not necessary, but it makes the code run slower. – Aston Jun 03 '16 at 15:43
0

symfony CLI tasks require quite a lot of PHP memory, especially on Windows. If the Propel task is failing, I would recommend a permanent change to the php.ini file setting on memory allocation to at least 256M. I know this seems high, but you should only ever need these tasks on a development machine.

Raise
  • 2,034
  • 1
  • 13
  • 12
0

You have some good tips in an other thread on SO.

And here is a really good blog post about memory leak using propel. It's in french, but it's really interesting.

And, if you are working on big data (such as mass import) you should also took a look at pcntl_fork (see this gist). pcntl_fork doesn't work on Windows. I used this method to deal with big import and it's really fast and don't eat all your memory.

Community
  • 1
  • 1
j0k
  • 22,600
  • 28
  • 79
  • 90