Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PM managed GC #6554

Merged
merged 8 commits into from
Dec 15, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/MemoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
use function fwrite;
use function gc_collect_cycles;
use function gc_disable;
use function gc_enable;
use function gc_mem_caches;
use function gc_status;
use function get_class;
use function get_declared_classes;
use function get_defined_functions;
Expand All @@ -55,6 +55,7 @@
use function is_resource;
use function is_string;
use function json_encode;
use function max;
use function mb_strtoupper;
use function min;
use function mkdir;
Expand Down Expand Up @@ -107,6 +108,7 @@ public function __construct(
$this->logger = new \PrefixedLogger($server->getLogger(), "Memory Manager");

$this->init($server->getConfigGroup());
gc_disable();
}

private function init(ServerConfigGroup $config) : void{
Expand Down Expand Up @@ -152,7 +154,6 @@ private function init(ServerConfigGroup $config) : void{
$this->lowMemClearWorldCache = $config->getPropertyBool(Yml::MEMORY_WORLD_CACHES_LOW_MEMORY_TRIGGER, true);

$this->dumpWorkers = $config->getPropertyBool(Yml::MEMORY_MEMORY_DUMP_DUMP_ASYNC_WORKER, true);
gc_enable();
}

public function isLowMemory() : bool{
Expand Down Expand Up @@ -204,6 +205,25 @@ public function trigger(int $memory, int $limit, bool $global = false, int $trig
$this->logger->debug(sprintf("Freed %gMB, $cycles cycles", round(($ev->getMemoryFreed() / 1024) / 1024, 2)));
}

private const GC_THRESHOLD_TRIGGER = 100;
private const GC_THRESHOLD_MAX = 1_000_000_000;
private const GC_THRESHOLD_DEFAULT = 10_001;
private const GC_THRESHOLD_STEP = 10_000;

private int $gcThreshold = self::GC_THRESHOLD_DEFAULT;

private function adjustGcThreshold(int $count, int $roots) : void{
//TODO Very simple heuristic for dynamic GC buffer resizing:
//If there are "too few" collections, increase the collection threshold
//by a fixed step
//Adapted from zend_gc.c/gc_adjust_threshold() as of PHP 8.3.14
if($count < self::GC_THRESHOLD_TRIGGER || $roots >= $this->gcThreshold){
$this->gcThreshold = min(self::GC_THRESHOLD_MAX, $this->gcThreshold + self::GC_THRESHOLD_STEP);
}elseif($this->gcThreshold > self::GC_THRESHOLD_DEFAULT){
$this->gcThreshold = max(self::GC_THRESHOLD_DEFAULT, $this->gcThreshold - self::GC_THRESHOLD_STEP);
}
}

/**
* Called every tick to update the memory manager state.
*/
Expand Down Expand Up @@ -239,6 +259,15 @@ public function check() : void{
if($this->garbageCollectionPeriod > 0 && ++$this->garbageCollectionTicker >= $this->garbageCollectionPeriod){
$this->garbageCollectionTicker = 0;
$this->triggerGarbageCollector();
}else{
$status = gc_status();
$roots = $status["roots"];
if($roots >= $this->gcThreshold){
Timings::$garbageCollector->startTiming();
$cycles = gc_collect_cycles();
$this->adjustGcThreshold($cycles, $roots);
Timings::$garbageCollector->stopTiming();
}
}

Timings::$memoryManager->stopTiming();
Expand Down Expand Up @@ -465,7 +494,6 @@ public static function dumpMemory(mixed $startingObject, string $outputFolder, i
$logger->info("Finished!");

ini_set('memory_limit', $hardLimit);
gc_enable();
}

/**
Expand Down
Loading