lithium
lithium copied to clipboard
Memory leak in Model
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 "
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 Thanks for that additional information and testing.
@chaiyanlin What PHP version did you use?