parallel icon indicating copy to clipboard operation
parallel copied to clipboard

Memory leak and high CPU usage

Open ziaratban opened this issue 4 years ago • 4 comments
trafficstars

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 avatar Jun 11 '21 20:06 ziaratban

@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.

dotfry avatar Jun 20 '21 20:06 dotfry

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
        }]);
    }
}

ziaratban avatar Jun 21 '21 06:06 ziaratban

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.

dotfry avatar Jun 29 '21 21:06 dotfry

Looks like it is insanely resource intensive (tons more than forking process)

https://news.ycombinator.com/item?id=36024818

faizanakram99 avatar Jan 19 '24 10:01 faizanakram99

Hey there @ziaratban, I assume your initial issue is solved? Thanks @dotfry for stepping in!

realFlowControl avatar May 13 '24 12:05 realFlowControl

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!

realFlowControl avatar May 13 '24 12:05 realFlowControl

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.

ziaratban avatar May 18 '24 04:05 ziaratban