Symfony task - memory leak

后端 未结 3 794
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-16 00:50

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 tes         


        
3条回答
  •  醉话见心
    2021-01-16 01:25

    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();
    }
    

提交回复
热议问题