Skip to content

Memory leak from namespace-level constants in worker mode #153

@zip-fa

Description

@zip-fa

Version

latest

Steps to Reproduce

  1. Install posthog/posthog-php in a Laravel application.
  2. Run the application using a long-lived worker runtime (FrankenPHP worker mode, Swoole, RoadRunner, or Laravel Octane).
  3. Send requests that trigger PostHog SDK calls (e.g. PostHog::capture(), PostHog::getFeatureFlag()).
  4. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions