Version
latest
Steps to Reproduce
- Install
posthog/posthog-php in a Laravel application.
- Run the application using a long-lived worker runtime (FrankenPHP worker mode, Swoole, RoadRunner, or Laravel Octane).
- Send requests that trigger PostHog SDK calls (e.g.
PostHog::capture(), PostHog::getFeatureFlag()).
- Monitor memory usage with
memory_get_usage() over thousands of requests.
Expected Result
Memory usage should remain stable across requests, since the worker process reuses the same PHP runtime without restarting.
Actual Result
Memory grows linearly with each request and is never reclaimed. The root cause is the namespace-level constants SIZE_LIMIT in lib/Client.php and LONG_SCALE in lib/FeatureFlag.php. In persistent worker runtimes, namespace-level constants are re-evaluated on every request and not garbage-collected between requests, causing a steady memory leak.
Moving these to private const on their respective classes (Client::SIZE_LIMIT, FeatureFlag::LONG_SCALE) ties them to the class autoloader cache, which is loaded once and reused — eliminating the leak.
Version
latest
Steps to Reproduce
posthog/posthog-phpin a Laravel application.PostHog::capture(),PostHog::getFeatureFlag()).memory_get_usage()over thousands of requests.Expected Result
Memory usage should remain stable across requests, since the worker process reuses the same PHP runtime without restarting.
Actual Result
Memory grows linearly with each request and is never reclaimed. The root cause is the namespace-level constants
SIZE_LIMITinlib/Client.phpandLONG_SCALEinlib/FeatureFlag.php. In persistent worker runtimes, namespace-level constants are re-evaluated on every request and not garbage-collected between requests, causing a steady memory leak.Moving these to
private conston their respective classes (Client::SIZE_LIMIT,FeatureFlag::LONG_SCALE) ties them to the class autoloader cache, which is loaded once and reused — eliminating the leak.