Skip to content

Conversation

@karstennilsen
Copy link

@karstennilsen karstennilsen commented Dec 27, 2025

Summary

  • Add frankenphp_min_memory_mb option to define minimum available memory (in MB) required before handling a new request
  • If available memory falls below threshold after the first cycle, worker restarts (like frankenphp_loop_max)
  • Configurable via FRANKENPHP_MIN_MEMORY_MB environment variable
  • Default -1 (disabled)
  • Updated README with documentation

How to test

  • Set FRANKENPHP_MIN_MEMORY_MB=64 and verify worker restarts when available memory drops below 64MB
  • Verify worker continues normally when memory is sufficient
  • Verify first request always processes regardless of memory

Introduce `frankenphp_min_memory_mb` option to enforce a minimum memory threshold before handling new requests. This prevents running workers when memory is critically low, enhancing stability. Option is configurable via environment variables or runtime options. Default value is -1 which disables this new functionality.
@karstennilsen
Copy link
Author

@Nyholm For our project this would be really helpful. What do you think about this?

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Great idea. But I made some minor comments


private function hasAvailableMemory(int $loops): bool
{
if (-1 === $this->minMemoryMb || 1 === $loops) { // Always allow the first cycle
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If 1 === $loops, then we have completed the first request. If we are out of memory after the first request, we should restart, right?

I suggest removing this check.

if (strlen($memoryLimit) < 2) {
return true; // Unexpected value, consider enough mem available
}
$last = \strtolower($memoryLimit[\strlen($memoryLimit) - 1]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that a valid ini settings is:

memory_limit=4711

This menas 4711 bytes. See https://www.php.net/manual/en/ini.core.php#ini.memory-limit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants