MemoryManager: Shut down idle workers during GC to reclaim memory

workers can be a major memory hog, especially if you have lots of them.
This commit is contained in:
Dylan K. Taylor 2018-07-14 18:07:37 +01:00
parent 878dd3b842
commit 2e18fe710c
2 changed files with 16 additions and 0 deletions

View File

@ -243,6 +243,9 @@ class MemoryManager{
if($this->garbageCollectionAsync){
$pool = $this->server->getAsyncPool();
if(($w = $pool->shutdownUnusedWorkers()) > 0){
$this->server->getLogger()->debug("Shut down $w idle async pool workers");
}
foreach($pool->getRunningWorkers() as $i){
$pool->submitTaskToWorker(new GarbageCollectionTask(), $i);
}

View File

@ -317,6 +317,19 @@ class AsyncPool{
$this->collectWorkers();
}
public function shutdownUnusedWorkers() : int{
$ret = 0;
foreach($this->workerUsage as $i => $usage){
if($usage === 0){
$this->workers[$i]->quit();
unset($this->workers[$i], $this->workerUsage[$i]);
$ret++;
}
}
return $ret;
}
/**
* Cancels all pending tasks and shuts down all the workers in the pool.
*/