Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP fibers and memory

Tags:

php-8.1

PHP loads classes into memory and never frees them as far as I am aware. This can be problematic when using attributes.

I am wondering whether fibers could be used. How independent is a fiber? Ultimately the question is this: if a PHP fiber loads a class, is that going to be autoloaded for the rest of the PHP code or is it a separate memory space?

like image 414
chx Avatar asked May 24 '26 10:05

chx


2 Answers

Code loaded during Fiber execution is available afterwards. Running the following code in Drupal bootstrapped environment:

var_dump(class_exists('\Drupal\Core\Action\Plugin\Action\DeleteAction', FALSE));
$fiber = new Fiber(function() : void {
  var_dump(class_exists('\Drupal\Core\Action\Plugin\Action\DeleteAction', TRUE));
});
$fiber->start();
var_dump(class_exists('\Drupal\Core\Action\Plugin\Action\DeleteAction', FALSE));

produces the following output:

bool(false)
bool(true)
bool(true)
like image 167
alexpott Avatar answered May 30 '26 03:05

alexpott


Fibers share the same memory space. Each fiber just has its own call stack. If you load a class in a fiber, it's loaded into the process heap, not in the fibers stack allocated memory.

like image 38
kelunik Avatar answered May 30 '26 03:05

kelunik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!