Skip to content

[VarExporter] Fix possible memory-leak when using lazy-objects #48461

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

Merged
merged 1 commit into from
Dec 4, 2022

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Dec 3, 2022

Q A
Branch? 6.2
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #48382
License MIT
Doc PR -

I would have loved to not store lazy-initializers in lazy objects, but this is not possible in PHP at the moment (see php/php-src#10043 for details).

This PR moves storing state of lazy objects inside the objects themselves. This fixes the memory leak by allowing the garbage collector to free memory as needed. This has the drawback of adding noise when var_dump($lazyObject), but at least we can make dump($lazyObject) aware of that, thus the change on VarDumper.

@stephpy
Copy link
Contributor

stephpy commented Dec 6, 2022

👍 I can confirm you fixed a memory leak.

I had test on behat with symfony 6.1, with: 2m35.82s (82.49Mb)

6.2 (tagged), i get 2m24.30s (2.12Gb)

6.2.x-master on var-exporter component and I get 2m37.99s (92.16Mb)

Looks nice :) Thanks.

@fabpot fabpot mentioned this pull request Dec 6, 2022
fabpot added a commit that referenced this pull request Dec 9, 2022
…r references in the container (nicolas-grekas)

This PR was merged into the 6.3 branch.

Discussion
----------

[DependencyInjection] Use WeakReference to break circular references in the container

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

While working on #48461, I realized that we could break many circular loops involving the container by using a `WeakReference` to access it. This happens when we generate closures.

Conceptually, we generate this at the moment:
```php
$internalClosure = function () { return $this->get('foo'); };
```

And we'd generate this with this PR:
```php
$containerRef = \WeakReference::create($this);
$internalClosure = static function () use ($containerRef) { return $containerRef->get()->get('foo'); };
```

This should reduce the pressure on the garbage collector.

Commits
-------

a3b7fca [DependencyInjection] Use WeakReference to break circular references in the container
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants