Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

Threaded Memory leak #681

Closed
tmtbe opened this issue Feb 22, 2017 · 6 comments
Closed

Threaded Memory leak #681

tmtbe opened this issue Feb 22, 2017 · 6 comments

Comments

@tmtbe
Copy link

tmtbe commented Feb 22, 2017

How to destroy Threaded?

<?php
ini_set('zend.enable_gc', true);
class Work extends Threaded {
    public function __construct()
    {
    }

    public function run(){
        print_r(1);
    }
}

$my = new Worker();
$work = new Work($my);
$my->start();
$my->stack($work);
cli_set_process_title('swdd');
while (true){
    $my->collect(function (Threaded $t)use($my){
        $work = new Work();
        $my->stack($work);
        return true;
    });
}

The above code is running, the memory will continue to rise.

@Jackysi
Copy link

Jackysi commented Feb 28, 2017

I've had same similar issue for months, too bad I have zero knowledge about C to fix this. I was hoping krakjoe has more time to fix this but sadly the author is just soo busy ^^. I can understand, but this extension is so useful for those that want to use PHP for simple daemons and have no knowledge or will to learn C/C++/C#

P.S.: I can't use extension for that fact which is very sad =(

@sirsnyder sirsnyder mentioned this issue Mar 8, 2017
@sirsnyder
Copy link
Collaborator

@tmtbe @Jackysi should be fixed, see my patch

@Jackysi
Copy link

Jackysi commented Mar 13, 2017

so this did fix the issue, I can't test right now cause I kinda removed the threading code?

@saniel
Copy link

saniel commented May 11, 2017

Hi guys, I've noticed the memory leak too. I'm using PHP 7.1 with pthreads from master branch cloned on april 27th.
I'm using following code:

<?php
$worker = new \Worker();
$worker->start();

sleep(3); // to get time to manually get memory usage

$i = 500000;
while ($i) {
	$i--;
	$work = new \Threaded();
	$worker->stack($work);
	
//	while ($worker->getStacked()) {};
//	$worker->collect();
}

while ($worker->getStacked()) {};
$worker->collect();

$worker->shutdown();

echo "done\n";
while (1) sleep(1);

I get memory usage from "ps aux".

Memory used just after starting new worker thread:
daniel 23561 1.3 0.3 287456 28772 pts/1 Sl+ 12:07 0:00 php test.php

Memory use when executed as it is:
daniel 23354 72.6 5.5 683352 448592 pts/1 S+ 11:55 0:05 php test.php

When I uncomment the commented code, i.e. wait for previous work to be done before stacking next I get significantly less memory used, but still growing indefinitely, when looping forever.
daniel 23114 10.4 0.9 284880 75700 pts/1 S+ 11:54 0:05 php test.php

When I create my own work class which stores some initial data in it's constuctor, the memory is consumed more.

I hope this helps a bit to identify source of the issue.

@saniel
Copy link

saniel commented May 11, 2017

So I just realized fix from @sirsnyder is not in the master. I tried his fork and it is much better. When stacking work sequentially (i.e. next only when the previous is done) memory usage remains constant.
When I stack all work first and collect it when all is done, memory usage grows. I assume it is the necessary stack size.

What is IMHO not correct, is that when the worker thread is shut down, memory is not freed.

sirsnyder added a commit to sirsnyder/pthreads that referenced this issue May 29, 2017
@tpunt
Copy link
Collaborator

tpunt commented Jun 1, 2017

PR #688 has now been merged into master, so this should be fixed now.

@tpunt tpunt closed this as completed Jun 1, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants