PHP fibers and memory

Viewed 281

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?

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)

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.

Related