Skip to content
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]: Model Validation Leaks Memory #16566

Open
ALameLlama opened this issue Apr 8, 2024 · 5 comments
Open

[BUG]: Model Validation Leaks Memory #16566

ALameLlama opened this issue Apr 8, 2024 · 5 comments
Assignees
Labels
bug A bug report status: unverified Unverified

Comments

@ALameLlama
Copy link
Contributor

Describe the bug
On a new phalcon project Trying to create models in a loop and the model has a validation function will leak memory

To Reproduce
I set up a user model and then added this like in the example

    public function validation()
    {
        $validator = new Validation();

        $validator->add(
            'email',
            new Uniqueness(
                [
                    'message' => 'The customer email must be unique',
                ]
            )
        );

        return $this->validate($validator);
    }

Created a loop like this

        $count = 100;
        for ($i = 0; $i <= $count; $i++) {
            $before = microtime(true);

            $user = new Users([
                'firstname' => 'John',
                'lastname' => 'Doe',
                'password' => 'securepassword',
                'is_active' => true,
            ]);

        $user->create();

            $after = microtime(true);

            echo sprintf(
                'Took %s seconds. Used %s memory',
                round($after - $before, 4),
                round(((memory_get_usage() / 1024) / 1024), 4) . 'M'
            ) . PHP_EOL;
        }

Over each model it's still holding a refereance to something

Took 0.0199 seconds. Used 1.6799M memory
Took 0.0182 seconds. Used 1.6803M memory
Took 0.0185 seconds. Used 1.6806M memory
Took 0.0182 seconds. Used 1.681M memory
...
Took 0.0179 seconds. Used 1.7147M memory
Took 0.0181 seconds. Used 1.715M memory
Took 0.0181 seconds. Used 1.7154M memory
Took 0.0185 seconds. Used 1.7158M memory
Took 0.0181 seconds. Used 1.7161M memory
Took 0.0187 seconds. Used 1.7165M memory

Expected behavior
I'm pretty sure this should get GC'd I checked with gc_status() and it looks like the PHP garbage collector is running but it's not freeing this memory

Details

  • Phalcon version: 5.6.1
  • PHP Version: 8.2.16
  • Operating System: Ubuntu
  • Installation type: Downloaded via git releases
  • Server: Apache
  • Other related info (Database, table schema): MariaDB
@ALameLlama ALameLlama added bug A bug report status: unverified Unverified labels Apr 8, 2024
@noone-silent
Copy link

I think I reported this Bug on the discord. It's not the validation, it's the model manager, who keeps a copy of every model and this grows unlimited

@maxgalbu
Copy link
Contributor

@noone-silent is this confirmed or it's just a guess? Do you know if someone is looking into it? I'm having memory leak problems in a forever-running CLI script

@noone-silent
Copy link

Yes, this is where you can experience it. We had the same problem, our container crashed, because the memory was only growing. I debugged it and found out, that the ModelsManager keeps every Model and the relations in memory.

So it is confirmed on our side. We overwrite the models to always use a new ModelsManager

    public function initialize(): void
    {
        // Only run this on the first initialization.
        // When there are non critical tasks, place it outside of this if.
        if ($this->modelsManager instanceof Manager === false) {
            $this->useDynamicUpdate(true);
            $this->modelsManager = new Manager();
            $this->modelsManager->setDI($this->getDI());
        }
    }

Also you need to overwrite the getRelated and postSaveRelatedRecords Methods. This solved our memory Problem.

@noone-silent
Copy link

@maxgalbu If this fixes your issues too, please post it here.

@Jeckerson Jeckerson self-assigned this Apr 28, 2024
@maxgalbu
Copy link
Contributor

@noone-silent actually, it wasn't models that were leaking for me, I did find another memory leak: #16571

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: unverified Unverified
Projects
None yet
Development

No branches or pull requests

4 participants