Cphalcon: Model::find memory problem in Phalcon 3.0 and PHP 7

Created on 15 Aug 2016  路  3Comments  路  Source: phalcon/cphalcon

The cyclic Model::find()/Model::findFirst() call produce a memory problem.

Test PHP code:

$loader = new \Phalcon\Loader();
$di = new \Phalcon\Di\FactoryDefault();
$di->set('db', function () use ($di) {
        $connection = new \Phalcon\Db\Adapter\Pdo\Mysql([
        /**/
    ]);
    return $connection;
}, true);

function bytes($size)
{
    $units = [' B', ' KB', ' MB', ' GB', ' TB'];
    for ($i = 0; $size >= 1024 && $i < 4; $i++)
        $size /= 1024;
    return round($size, 2) . $units[$i];
}

class Country extends \Phalcon\Mvc\Model
{
    public $id;
    public $name;
    public $flagpic;
    public $domain;
    public function getSource()
    {
        return 'countries';
    }
}
$counter = 0;
while (true) {
    $model = Country::findFirst();
    if ($counter % 1000 == 0) {
        echo bytes(memory_get_usage()) . "\n";
    }
    if ($counter > 10000) {
        die();
    }
    $counter++;
}

PHP 7.0

  • Phalcon version: 3.0.0
  • PHP Version: 7.0.9
  • Operating System: Ubuntu 16.04
  • Installation type: Compiling from source (branch 3.0.x commit 649f35c9b7225b8110fd8d1e27d32434e576e058)
  • Server: run in console

result:

389.09 KB
3.45 MB
6.52 MB
9.62 MB
12.65 MB
15.82 MB
18.85 MB
21.89 MB
24.93 MB
28.21 MB
31.25 MB

PHP 5.5

  • Phalcon version: 3.0.0
  • PHP Version: 5.5.9
  • Operating System: Ubuntu 14.04
  • Installation type: apt repository
  • Server: run in console

result:

301.1 KB
301.26 KB
301.26 KB
301.26 KB
301.26 KB
301.26 KB
301.26 KB
301.26 KB
301.26 KB
301.26 KB
301.26 KB

Valgrind log is empty.

bug medium

Most helpful comment

Okay. There is some problem with phql internal cache(but it shouldn't even work i think on php 7, maybe make it working properly @andresgutierrez ?). It looks like queries are added to some array for caching, but they are never checked if they already exists and they are not used i think. After adding this after findFirst:

$di->get('modelsManager')->__destruct();

It works fine. This is workaround for now.

Also it's not memory leak as it is, just some mistake somewhere in https://github.com/phalcon/cphalcon/blob/master/ext/phalcon/mvc/model/query/base.c or https://github.com/phalcon/cphalcon/blob/master/ext/phalcon/mvc/model/query/parser.c

I think it's caused by this line perhaps - https://github.com/phalcon/cphalcon/blob/master/ext/phalcon/mvc/model/query/base.c#L662 but im guessing

I can confirm by removing this instruction the problem is fixed but i think it's a workaround still, i think we should make phql cache for working with php 7 too.

Well i think for now the best will be just commenting out this in 3.0.1, and in 3.x.0 make it working like in php5.

All 3 comments

Okay. There is some problem with phql internal cache(but it shouldn't even work i think on php 7, maybe make it working properly @andresgutierrez ?). It looks like queries are added to some array for caching, but they are never checked if they already exists and they are not used i think. After adding this after findFirst:

$di->get('modelsManager')->__destruct();

It works fine. This is workaround for now.

Also it's not memory leak as it is, just some mistake somewhere in https://github.com/phalcon/cphalcon/blob/master/ext/phalcon/mvc/model/query/base.c or https://github.com/phalcon/cphalcon/blob/master/ext/phalcon/mvc/model/query/parser.c

I think it's caused by this line perhaps - https://github.com/phalcon/cphalcon/blob/master/ext/phalcon/mvc/model/query/base.c#L662 but im guessing

I can confirm by removing this instruction the problem is fixed but i think it's a workaround still, i think we should make phql cache for working with php 7 too.

Well i think for now the best will be just commenting out this in 3.0.1, and in 3.x.0 make it working like in php5.

I guess it's fixed on 3.0.1 @andresgutierrez ? Can you check it @dimak08

@Jurigag Thanks, problem was fixed (288161da4009c06b19c1608f0d9b10d39d398915).

Was this page helpful?
0 / 5 - 0 ratings