Skip to content

Commit 0310499

Browse files
committed
minor #16798 [Process] Unset callback after stop to free memory (Slamdunk)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #16798). Discussion ---------- [Process] Unset callback after stop to free memory | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - As of yet, a `Process` instance can't free memory. The built-in callback has a self-reference to the intance not cleared after instance destruction. I've tried to change both `buildCallback` and https://github.com/symfony/process/blob/v2.7.6/Process.php#L1373 usage to avoid self-referencing, but it can't be done without breaking BC, and the memory issue is still there. A simple test script (that obiously can't be embedded in unit-tests) ```php for ($inc = 0; $inc < 10000; $inc++) { $process = new Symfony\Component\Process\Process('echo 1'); $process->mustRun(); $process->stop(); unset($process); echo (memory_get_usage(true) / 1000000) . ' MB ' . $inc . PHP_EOL; } ``` Before: ``` 1.572864 MB 0 5.505024 MB 9999 ``` After: ``` 1.572864 MB 0 1.572864 MB 9999 ``` Ok, in this simple scenario 4 MB of RAM is nothing, but in our business app full of IOC where `unset`-ting is hard, our RAM explodes. After `stop()`, the callback is no longer necessary. To be clear, the garbage collector in the previous example was already active. Deactivating it manually, which somewhere is needed to avoid particluar segfaults, obiously leads to worse scenarios: Before: ``` gc_disable(); 1.572864 MB 0 29.360128 MB 9999 ``` After: ``` gc_disable(); 1.572864 MB 0 1.572864 MB 9999 ``` Commits ------- ec93b9a [Process] Unset callback after stop to free memory
2 parents fbc353d + ec93b9a commit 0310499

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/Symfony/Component/Process/Process.php

+5
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,11 @@ private function close()
11361136
$this->exitcode = 128 + $this->processInformation['termsig'];
11371137
}
11381138

1139+
// Free memory from self-reference callback created by buildCallback
1140+
// Doing so in other contexts like __destruct or by garbage collector is ineffective
1141+
// Now pipes are closed, so the callback is no longer necessary
1142+
$this->callback = null;
1143+
11391144
return $this->exitcode;
11401145
}
11411146

0 commit comments

Comments
 (0)