krakjoe / pthreads

Threading for PHP - Share Nothing, Do Everything :)
Other
3.47k stars 503 forks source link

memory leak when pthreads in pthreads #954

Open Still4 opened 4 years ago

Still4 commented 4 years ago
<?php

$worker_num_total = 40;
$global_data = array();
$variables_list = array_pad(array(), 1000000, 0);
$variables = array_chunk($variables_list, 40);
foreach ($variables as $key => $chunks) {
    $pool = new \Pool($worker_num_total, \Worker::class);
    $pool->submit(new SecondThread($chunks));
    while (($worker_num = $pool->collect()) > 0) {
        usleep(100000);
    }
    $pool->shutdown();
    unset($pool);
    gc_collect_cycles();
}

class SecondThread extends \Thread
{

    private $chunks;

    public function __construct($chunks)
    {
        $this->chunks = $chunks;
    }

    public function run()
    {
        $pool = new \Pool(10, \Worker::class);
        foreach ($this->chunks as $chunk) {
            $pool->submit(new ThirdThread($chunk));
        }
        while (($worker_num = $pool->collect()) > 0) {
            usleep(100000);
        }
        $pool->shutdown();
        unset($pool);
        gc_collect_cycles();
    }

}

class ThirdThread extends \Thread
{

    private $chunk;

    public function __construct($chunk)
    {
        $this->chunk = $chunk;
    }

    public function run()
    {
        $this->chunk;
    }

}

?>

the first minute

image

about 5 mins later

image

about 10 mins later

image
Still4 commented 4 years ago

php versoin PHP 7.2.6 (cli) (built: Jun 1 2018 07:54:23) ( ZTS ) pthread 3.1.7dev