parallel
parallel copied to clipboard
Memory leak and high CPU usage
Hi. In the continuation of this issue :
a.php
<?php
spl_autoload_register(function(){
require __DIR__.'/b.php';
});
$b = new B();
$b->test = $b; #Remove this line to fix the bug
$b->run();
b.php
<?php
class B {
public function run(){
echo 'First'.PHP_EOL; #printed
\parallel\run(function($worker){
echo 'Second'.PHP_EOL; #not printed
$worker();
},[function(){
echo 'Third'.PHP_EOL; #not printed
}]);
}
}
CPU and RAM leaked after run this script.
@krakjoe @dotfry
@ziaratban, hi read issue #64 about coping variables. also try to make all closures static. atm, your class B binded to closure that goes into \parallel\run function.
Thanks.
I can fix this bug by calling the thread in another class.
b.php
<?php
class B {
public function run(){
C::Fix();
}
}
class C {
public static function Fix():void{
echo 'First'.PHP_EOL; #printed
\parallel\run(function($worker){
echo 'Second'.PHP_EOL; #printed
$worker();
},[function(){
echo 'Third'.PHP_EOL; #printed
}]);
}
}
I mean something like:
<?php
class B {
public function run(){
echo 'First'.PHP_EOL; #printed
\parallel\run(static function($worker){
echo 'Second'.PHP_EOL;
$worker();
},[static function(){
echo 'Third'.PHP_EOL;
}]);
}
}
for your original example.
Looks like it is insanely resource intensive (tons more than forking process)
https://news.ycombinator.com/item?id=36024818
Hey there @ziaratban, I assume your initial issue is solved? Thanks @dotfry for stepping in!
Looks like it is insanely resource intensive (tons more than forking process)
https://news.ycombinator.com/item?id=36024818
Regarding
- 10k: 8.3 GB!
If for 10k threads the PHP process allocates 8.3 GB, that is 0.83 MB per thread and I would say that this is everything but expensive.
I should add that the good ole' fork approach[1] only uses 40.7mb for 10k, 40.7mb for 100k, and 40.7mb for 1 million.
pcntl_fork() spawns new processes, which memory is not allocated in the parent, but in the child processes themself. This means that in the parent process, the memory consumption will be stable, no matter how many children you spawn, but every children will allocate it's own memory (and it will be more than 0.83 MB, not quite the full ~40 MB because some parts will be shared by the operating system).
Hope I could help!
Hey there @realFlowControl I will not continue working on this project. But I hope that the parallel project will be moved to php core. My opinion is that php should grow uniquely on the three important axes of speed, dx(on syntax) and background process.