Skip to content

High memory use when analysing large literal arrays #15452

Open
phpstan/phpstan-src
#6727

Description

@alfredbez

Bug report

Analysing a PHP array literal with 20,000 string keys and values costs about 120 MB and 1.2 s for one file on PHPStan 2.3.1.

Reproducer: https://github.com/alfredbez/phpstan-literal-array-memory-reproducer

It generates two files that return the same data. One contains the array literal, the other loads it from a JSON file with json_decode(file_get_contents(...)). Each file is analysed in a separate process with an empty cache, at level 6 with --debug -vvv.

Results on macOS arm64, PHP 8.5.10, Xdebug disabled, 20,000 entries (medians of three runs):

PHPStan Array literal JSON control
2.3.0 121.5 MB, 1.19 s 9.98 MB, 0.07 s
2.3.1 121.5 MB, 1.19 s 7.98 MB, 0.07 s

These are the per-file "consumed" values from --debug -vvv: the increase in memory_get_peak_usage(true) during analysis, not retained memory or RSS. All analyses exit 0.

The JSON variant does not require PHPStan to infer every entry, so it does less static-analysis work. I do not expect the same cost, but I would like to see whether the literal-array case can use less memory without losing useful type information. This comparison does not show a regression between 2.3.0 and 2.3.1 or a memory leak.

I already opened phpstan/phpstan-src#6727 with a proposed fix. Opening this issue with the reproducer as requested there.

Code snippet that reproduces the problem

git clone https://github.com/alfredbez/phpstan-literal-array-memory-reproducer.git
cd phpstan-literal-array-memory-reproducer
composer install
XDEBUG_MODE=off php run.php

Requires PHP 8.2 or newer and Composer. The repository pins PHPStan 2.3.1. To use another PHPStan version or a larger array:

XDEBUG_MODE=off php run.php /path/to/phpstan.phar 40000

Expected output

Lower peak memory use when analysing large literal arrays, without losing useful type information. I am happy to test a fix against the reproducer.

Activity

  1. mergeable commented on Oct 10, 2026

    @mergeable

    This bug report is missing a link to reproduction at phpstan.org/try.

    It will most likely be closed after manual review.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions