Skip to content
Latchkey

PHP "Allowed memory size exhausted" (PHP CLI) in CI

PHP enforces a per-process memory_limit. When a CLI script - a migration, a data import, a build task - allocates more than that limit, PHP aborts with a fatal "Allowed memory size exhausted". In CI the CLI php.ini limit is often lower than you expect.

What this error means

A PHP CLI step dies with "PHP Fatal error: Allowed memory size of N bytes exhausted (tried to allocate M bytes)". The same script may run fine locally where memory_limit is higher.

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

Common causes

CLI memory_limit too low for the workload

The CLI php.ini caps memory below what the script needs (large datasets, big arrays), so PHP aborts.

Unbounded data loaded into memory

The script reads an entire result set or file into memory at once instead of streaming, spiking past the limit.

How to fix it

Raise the CLI memory_limit for the step

Pass a higher limit on the command line so the run has the memory it needs.

Terminal
php -d memory_limit=512M bin/import.php
# or set it in the CI php.ini via setup-php ini-values

Reduce peak memory in the script

  1. Stream or paginate large datasets instead of loading them all at once.
  2. Unset large variables and free results between iterations.
  3. Use generators for big collections so memory stays flat.

How to prevent it

  • Set an appropriate CLI memory_limit in CI for memory-heavy scripts.
  • Stream and paginate large datasets rather than loading them whole.
  • Use a runner sized for your heaviest CLI task.

Related guides

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