lithium icon indicating copy to clipboard operation
lithium copied to clipboard

Memory leak in Model

Open chaiyanlin opened this issue 6 years ago • 2 comments

Here is my case: I wrote a script as a consumer of Message Queue. But I found the memory usage will be increased after called Model::find().

e.g.

<?php
namespace app\extensions\command;

use app\models\Member;


class Test extends \lithium\console\Command
{

    public $openid;

    public function run()
    {
        while(true) {
            Member::findById(3);
            sleep(1);
            var_dump(str_pad(round(memory_get_usage(true) / (1024 * 1024), 2) . "M", 7));
        }
    }
}

output:

libraries/lithium/console/li3 test --env=test
string(7) "4M     "
string(7) "4M     "
string(7) "4M     "
string(7) "4M     "
string(7) "4M     "
string(7) "4M     "
string(7) "4M     "
string(7) "4M     "
string(7) "4M     "
string(7) "4M     "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.25M  "
string(7) "4.5M   "
string(7) "4.5M   "
string(7) "4.5M   "

chaiyanlin avatar Jan 29 '18 12:01 chaiyanlin

I could not reproduce this. Perhaps not enough time was allowed for the GC to do its job? I tested with a MongoDB connection using the same code as yourself for the cli command. For me, it does as is explained, starting from 3 or 4M and rises to between 8 and 10M depending on the machine it was tested on, but then drops back to around 6M once the GC kicks in. Rising and falling until I kill it.

I tested on the following Lithium versions (framework and library versions matched):

  • 1.0.0
  • 1.0.1
  • 1.0.2
  • 1.1.0
  • 1.1.1

I tested all on PHP 5.6.33 with MongoDB 3.4 and a coworker tested 1.0.x versions with PHP 5.3.3 with MongoDB 3.2 and some version of MySQL.

bengearig avatar Mar 16 '18 19:03 bengearig

@bengearig Thanks for that additional information and testing.

@chaiyanlin What PHP version did you use?

d1rk avatar Mar 31 '18 07:03 d1rk