pmmp / PocketMine-MP

A server software for Minecraft: Bedrock Edition in PHP
https://pmmp.io
GNU Lesser General Public License v3.0
3.28k stars 1.56k forks source link

Disable server thread automatic cyclic garbage collection, and let PM MemoryManager exclusively decide when to run GC #5628

Open dktapps opened 1 year ago

dktapps commented 1 year ago

Description

As of PHP 7.3, GC roots are always recorded. Before 7.3, only the first 10k roots were recorded while automatic GC was turned off, and the remainder would be ignored, leading to potential memory leaks.

Since the GC can be triggered automatically seemingly at random when the GC root buffer reaches a certain size, seemingly random lag spikes can occur in code that wouldn't otherwise be slow, causing weird outputs on timings reports.

Instead, I think it makes sense to disable automatic GC and have the server decide when to do it. This would have the following benefits:

Downsides:

dktapps commented 1 year ago

My initial attempts at this involved using gc_disable(), and then temporarily enabling it during MemoryManager::tick(), and then attempting to trigger GC by destroying an object to allow PHP's regular GC heuristics to decide whether to try running GC or not. However, this was unsuccessful, since I wasn't able to find a way to trigger a GC run without creating a cycle, which adds objects to the GC root buffer and ends up triggering more frequent GC runs.

dktapps commented 1 year ago

This may become viable with PHP 8.3 thanks to new information added to gc_status()