-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG]: Phalcon\Support classes leak memory #16571
Comments
That should be normal to be honest. The helper factory is just that, a factory that creates additional classes. When you instantiate the factory, you have one object. Once you call say |
Actually I take that back. That is what should be happening but it is not. The mapper is not reusing the object. I will sort that out shortly |
I run the same test with some variations. In the test you have, you are creating the helper factory and then calling
After the change I made (PR will be coming shortly) I got these results Your test as is:
Create factory outside the loop
Create the factory outside the loop and call
|
Resolved |
I'm creating the helper factory but I'm also storing every instance on the same variable With the following userland code mimicking HelperFactory from phalcon, the memory increases linearly just like using ucwords: <?php
echo "beginning: ".memory_get_peak_usage() / 1024 / 1024;
echo "\n";
class Camelize
{
public function __invoke(
string $text,
string $delimiters = null,
bool $lowerFirst = false
): string {
if (!$delimiters) {
$delimiters = "\n\t";
}
return str_replace(str_split($delimiters), "", ucwords($text, $delimiters));
}
}
class HelperFactory
{
private array $services = [];
public function __call(string $name, array $arguments)
{
$helper = $this->newInstance($name);
return call_user_func_array([$helper, "__invoke"], $arguments);
}
public function newInstance(string $name)
{
if (!isset($this->services[$name])) {
$instanceName = $this->getService($name);
$this->services[$name] = new $instanceName;
}
return $this->services[$name];
}
protected function getService($name)
{
return [
"camelize" => "Camelize",
][$name];
}
}
foreach (range(1,100000) as $num) {
$helper = new HelperFactory();
$helper->camelize("transaction_id");
}
echo "end: ".memory_get_peak_usage() / 1024 / 1024;
echo "\n"; Output:
Looking at these results, there seems to be no improvements at all? At least with |
@niden did you look at my comment? maybe you just mis-copied results after your fix? |
Describe the bug
Instantiating and invoking a Phalcon/Support class make memory grow too much.
I'm using it in a forever-running CLI script.
To Reproduce
Script to reproduce the behavior:
Output:
Expected behavior
Memory should not increase so much. Same output using ucwords():
Details
The text was updated successfully, but these errors were encountered: