Era Host hosting
EraHost – Free Domain, Cheap Hosting!
Client Area
Support 24/7
Menu

Set PHP memory_limit = -1 via Command Line (For CLI Scripts)

2 min read
24.05.2025

If you are running a PHP script via the command line (CLI) and need to remove the memory limit, you can do it dynamically without modifying php.ini.

PHP CLI memory_limit -1
`php -d memory_limit=-1 script.php` — one-shot unlimited.

For closely related memory_limit topics, see How to Update PHP Memory Limit for Magento 2, Magento CLI PHP Memory Limit, and Increasing PHP Limits.

Command to Set Memory Limit in CLI

Run your PHP script with unlimited memory:

php -d memory_limit=-1 script.php

How It Works:

  • php > Executes the PHP interpreter.
  • -d memory_limit=-1 > Overrides the memory_limit setting only for this execution.
  • script.php > Your PHP script.

Verify the Change in Your Script

Inside script.php, add:

<?php

echo "Memory Limit: " . ini_get('memory_limit') . PHP_EOL;

?>

Run:

php -d memory_limit=-1 script.php

Expected output:

Memory Limit: -1
Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

Alternative: Modify PHP Script Instead

If you cannot use command-line flags, modify the script:

<?php

ini_set('memory_limit', '-1');

echo "Memory Limit: " . ini_get('memory_limit');

?>

When to Use This Method

  • Best for one-time executions where you need unlimited memory.
  • Useful for long-running scripts (e.g., backups, large data processing).
  • Works without modifying php.ini or .htaccess.
  • Not recommended for web server environments (Apache/Nginx).
  • Can cause excessive memory usage if not monitored.

Setting a High but Limited Memory Instead

Instead of unlimited memory, you can set a large value:

php -d memory_limit=1G script.php

or

ini_set('memory_limit', '1G');
  • Use php -d memory_limit=-1 script.php for CLI execution.
  • Works without modifying php.ini.
  • Be cautious with unlimited memory usage in production environments.
Frequently asked questions
Risky. Worker with memory leak silently grows until OOM kills it (taking the host with it if leak is fast). Better: bounded limit (`-d memory_limit=2G`) + supervisor that restarts on exit. Worker exits when limit hit (OOM exception in PHP), supervisor restarts, leak doesn't accumulate.
Yes for deploy/maintenance scripts running for minutes. setup:upgrade, data migration, large CSV import. They start, finish, exit. Memory peak is bounded by what the script actually does. No risk of unbounded growth.
`PHP_INI_SCAN_DIR=/etc/php/8.1/cli/conf.d.unlimited` causes PHP to load additional .ini files from that directory. Put a `memory.ini` there with `memory_limit = -1`. Cleaner than `-d` for repeated commands. Useful in wrapper scripts that always run the same way.
Wrong place. `ini_get('memory_limit')` returns the set value, but if you set it at script top with `ini_set`, the cap applies post-set. Already-allocated memory at that point counted under previous limit. For early-OOM, set on command line (`-d`) so it applies from start.
Related articles
Change memory_limit via ini_set() in a PHP Script (Runtime Override)
PHP memory_limit = -1: What Does It Mean?
Fixing "A Fatal Error or Timeout Occurred" by Increasing PHP Limits