Phalcon v1.3.2 Memory leak in Phalcon\Mvc\Model::findFirst
In phalcon version 1.3.2 if I'm using in loop this function, script leak more memory each time
function getRow($id) {
$model = new \Model\Mysql\Mymodel();
$model->setReadConnectionService($adapter);
$model->setWriteConnectionService($adapter);
$row = $model->findFirst(["id = '".$id."'", 'order' => "taken_at DESC"]);
}
for other versions the same problem
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
Your function returns nothing?
I think it's normal to see memory growing since you create a new object each time via the findFirst method, isnit?
I've same problem in 1.3.1, whenever the findFirst function is called repeatedly, memory is growing dramatically.
it's only example, this function return nothing, that means after finish all memory that was use by this function should be free, but we have memory leak
the same problem with Phalcon\Mvc\Model::find(), it is very sadly
Look like Phalcon uses a "memory pool" or a "cache system".
In https://github.com/phalcon/cphalcon/blob/master/ext/mvc/model.c#L1442 you can see the call to the function PHALCON_MM_GROW which seem here to allocate memory (I am not sure).
Maybe there is a garbage collector function to restore unused memory (could be in https://github.com/phalcon/cphalcon/blob/master/ext/kernel/memory.c file).
What about add
unset($row);
?
unset($row);
it's not help
I'm seeing the same memory leak over here. It looks like there's no trivial way of freeing up the memory anymore and the app inevitably runs into an out of memory exception. This is consistently reproducible not only on Phalcon 1.3.x but already on Phalcon 1.2.6.
I wouldn't name this a memory leak: there's no memory space lost, as the data is still there. But I'm okay it's a bug, as you can't unset Model objects, to free up memory.
@ebuildy: I'm facing the issue as I am importing 300k users in a database. I have a model for each user, to create or update the database (the script can be run multiple times). As the data is to be found through PHP, that's a good approach to me, but I'm hitting 500MB of RAM really fast...
So please add $robot->free() or unset($robot), that would be great!
same problem
Could you please try again with 2.0.0?
Also the problem is not using bound parameters: http://docs.phalconphp.com/en/latest/reference/models-cache.html#caching-of-phql-planning
It seems better (on 2.0.0), and I use bound parameters everywhere:
var_dump(memory_get_usage());
$movie = Movie::findFirst(1234);
var_dump(memory_get_usage());
unset($movie);
var_dump(memory_get_usage());
871608
880152
872280 <= lost only 672 bytes
@dugwood Data is not "lost", PHQL planning is saved in memory until the request ends, you can track real memory leaks compiling PHP manually with --enable-debug and -dreport_memleaks=1
@phalcon I know there was PHQL, so in fact I did run Movie::findFirst() once before this test. I also know it's not a memory leak: https://github.com/phalcon/cphalcon/issues/2705#issuecomment-57840782
-- Removed some wrong tests --
Anyway, it seems to work, so it's okay to me. Also, I had issues on cron tasks on phalcon 1.3.4, script was slowing down (by 95%!) as soon as memory_get_usage() was approaching 100MB. There's no such issue on phalcon 2, and that's great!
php 5.6.3 phalcon Version => 2.0.0b Powered by Zephir => Version 0.5.9a
foreach ($array as $val) {
$model = new \My\Model();
$model->setReadConnectionService($modelAdapter);
$model->find();
}
"PHP Fatal error: Allowed memory size"
what is my problem?
Your problem has nothing to do with this real issue. When you're running $model->find(), you're loading the whole table into memory... Why?
In the table 700 rows, Fatal error on the ~2000 iteration, so that means that phalcon still caching
You're iterating on a foreach () that calls the $model->find() that calls all of your rows... Isn't it strange? What is $array?
Why do you have to load all 700 rows? Why don't you flush the models using unset($model) once you're done with it?
This is not a Phalcon issue, this is what you're doing which seems wrong.
so ok, more details, I use my own strong logic, with sharding module and in backend process (cron) I update search index, so on one of step I have query builder with something like this "WHERE condition1 = 'value1' AND condition2 = 'value2' AND condition2 = 'value2', I don't use bind params, I can't use in my logic bind params, in the end I have fatal error each time, but before when I don't need use conditions, and use only offsets and limits I haven't this problem, because phalcon cached query by "WHERE" part. what should I so, refactor my code and try to integrate binding or I have another choice?
Binding or not binding is up to you. That won't prevent a big loop from eating all your memory. You have to deal with your models: either you unset() the model when you're done with it, or you increase the memory limit. There's no other option when you're running big scripts. Or just restart the script when it hits too much RAM (see memory_get_usage()).