Comments (6)
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 camelize
on it, it will create the Camelize
object transparently (and the memory will increase by doing that) and then use it to produce the result. Any subsequent calls to camelize
will not increase the memory since it is already instantiated.
from cphalcon.
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
from cphalcon.
I run the same test with some variations.
In the test you have, you are creating the helper factory and then calling camelize
which in turn will create a new Camelize
object. This is in the loop so you are pretty much testing what is the memory consumption of two objects being created in a loop, again and again, so this does not represent a memory leak. The better tests are as follows (to see if there is a memory leak or not)
- Create the factory outside the loop, run the loop with the
camelize
in it, and see if the memory keeps increasing - Create the factory and call
camelize
outside the loop, then callcamelize
in the loop.
After the change I made (PR will be coming shortly) I got these results
Your test as is:
beginning: 0.41312408447266
end: 35.912994384766
Create factory outside the loop
beginning: 0.41301727294922
end: 35.903160095215
Create the factory outside the loop and call camelize
, then have camelize
in the loop
beginning: 0.41147613525391
end: 35.903717041016
from cphalcon.
Resolved
from cphalcon.
In the test you have, you are creating the helper factory and then calling camelize which in turn will create a new Camelize object. This is in the loop so you are pretty much testing what is the memory consumption of two objects being created in a loop, again and again, so this does not represent a memory leak.
I'm creating the helper factory but I'm also storing every instance on the same variable $helper
. Shouldn't this call the destructor on the old Factory instance every time I instantiate another Factory? Why is the old instance kept in memory?
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:
beginning: 0.41677856445312
end: 4.3862762451172
After the change I made (PR will be coming shortly) I got these results
Looking at these results, there seems to be no improvements at all? At least with Create factory outside the loop
there should be some improvements..
from cphalcon.
@niden did you look at my comment? maybe you just mis-copied results after your fix?
from cphalcon.
Related Issues (20)
- [NFR]: Add precompiled binaries for Linux and macOS ARM64
- [NFR]: Resetting query filters for query builder HOT 1
- [BUG]: Router parts cannot be unset, if matches parts are optional
- [BUG]: IncludedMaximum & IncludedMinimum behavior inverted for StringLength, Min, Max validation HOT 1
- Broken link https://phalcon.io/en-us/download/windows [NFR]: HOT 1
- [BUG]: Model Validation Leaks Memory HOT 5
- [BUG]: Form with field that is not part of the Model raised deprecated warning on bind() HOT 2
- [NFR]: ORM WITH JSON_EXTRACt HOT 1
- [BUG]: phalcon devtools not working HOT 3
- [NFR]: Assets which respect the base static URI from `\Phalcon\Mvc\Url`
- [BUG]: "SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens" produced when you use the same named binded parameter more than once in query conditions HOT 4
- [DOC] - Remove dead links in CONTRIBUTING.md HOT 1
- [NFR]: Implement autocompletion support for modern IDE HOT 2
- [BUG]: Discrepancy Between Total Items in Paginator and Direct Query Using Phalcon's QueryBuilder HOT 8
- [BUG]: Phalcon\Filter\Sanitize\BoolVal is not working as expected. HOT 1
- [BUG]: Classes that implement Serializable have wrong function declaration HOT 1
- [BUG]: Phalcon\Support classes leak memory round 2 HOT 8
- [NFR]: can you make wordpress optimized for phalcon or as รง.compiled alongwoth conpiled superspeed caching
- Phalcon 5.7.0 Release Reports 5.6.2 as Internal Version Number HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cphalcon.