Skip to content
Latchkey

PHP CLI "Allowed memory size exhausted" - Raise memory_limit in CI

A PHP CLI process - a console command, migration, or build step - exceeded PHP’s memory_limit and was killed. CLI PHP often inherits a finite limit from php.ini, and a memory-heavy command can blow past it in CI.

What this error means

A php bin/... command dies partway with "Allowed memory size of N bytes exhausted (tried to allocate …)". The same command may pass locally where the CLI memory_limit is higher or -1.

PHP error
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to
allocate 4096 bytes) in /app/src/Importer.php on line 88

Common causes

CLI memory_limit too low for the workload

The runner’s php.ini sets a finite CLI memory_limit (e.g. 128M). A bulk import, large dataset, or in-memory accumulation exceeds it.

Unbounded growth in the command

Loading an entire result set, building a giant array, or not freeing objects in a loop makes memory climb until it hits the limit.

How to fix it

Raise the limit for that command

Pass a higher (or unlimited) limit to the specific CLI invocation.

Terminal
php -d memory_limit=512M bin/console app:import
# or unlimited for a one-off batch job
php -d memory_limit=-1 bin/console app:import

Set a CLI default in the runner config

If many commands need headroom, raise the CLI memory_limit once.

php.ini
# php.ini (CLI SAPI)
memory_limit = 512M

Reduce memory use in the command

  1. Stream/batch large datasets instead of loading everything at once.
  2. Free large objects and detach ORM entities inside loops.
  3. Profile with memory_get_peak_usage() to find the spike.

How to prevent it

  • Set a sensible CLI memory_limit in the runner image.
  • Batch/stream large data instead of buffering it all in memory.
  • Add -d memory_limit to known heavy CLI steps in CI.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →