Skip to content

Commit

Permalink
bug #34918 [Translation] fix memoryleak in PhpFileLoader (nicolas-gre…
Browse files Browse the repository at this point in the history
…kas)

This PR was merged into the 3.4 branch.

Discussion
----------

[Translation] fix memoryleak in PhpFileLoader

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

This happens when running the test suite with opcache disabled (as it is the case by default since `opcache.enable_cli=0`).
Doing this "require" in a loop (for each test case) compiles the file at each iteration and doesn't reclaim memory (there is no garbage collector for opcodes).

Commits
-------

5c9e3ba [Translation] fix memoryleak in PhpFileLoader
  • Loading branch information
fabpot committed Dec 11, 2019
2 parents c90cf33 + 5c9e3ba commit 349ea04
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Symfony/Component/Translation/Loader/PhpFileLoader.php
Expand Up @@ -18,11 +18,25 @@
*/
class PhpFileLoader extends FileLoader
{
private static $cache = [];

/**
* {@inheritdoc}
*/
protected function loadResource($resource)
{
return require $resource;
if ([] === self::$cache && \function_exists('opcache_invalidate') && filter_var(ini_get('opcache.enable'), FILTER_VALIDATE_BOOLEAN) && (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) || filter_var(ini_get('opcache.enable_cli'), FILTER_VALIDATE_BOOLEAN))) {
self::$cache = null;
}

if (null === self::$cache) {
return require $resource;
}

if (isset(self::$cache[$resource])) {
return self::$cache[$resource];
}

return self::$cache[$resource] = require $resource;
}
}

0 comments on commit 349ea04

Please sign in to comment.