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

Change memory_limit via ini_set() in a PHP Script (Runtime Override)

2 min read
11.03.2025

If you want to remove the memory limit only for a specific PHP script, you can use the ini_set() function instead of modifying the global php.ini file.

ini_set memory_limit Runtime
ini_set at script top — per-request raise; not for OOM-on-load.

For closely related memory_limit topics, see Modify php.ini to Set memory_limit = -1, Set memory_limit = -1 via Command Line, Increasing PHP Limits, and How to Update PHP Memory Limit for Magento 2.

Example: Set memory_limit = -1 in a PHP Script

Add this line at the beginning of your PHP script:

<?php

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



// Example: Process large data

$data = str_repeat("A", 1024 * 1024 * 100); // 100MB of data

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

?>

What This Does:

  • ini_set('memory_limit', '-1'); removes all memory limits for this script only.
  • ini_get('memory_limit'); confirms that the limit has changed.

Verify the Change

Run the script and check the output. It should display:

Memory Limit Set to: -1

Alternatively, use:

<?php

echo "Current memory limit: " . ini_get('memory_limit');

?>
Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

When to Use ini_set('memory_limit', '-1')

  • Best for temporary needs (large file processing, database queries, backups).
  • Good for CLI scripts where you have control over execution.
  • Not recommended in production unless necessary (it may cause excessive memory use).
  • Does not work if memory_limit is set in php.ini with PHP_INI_SYSTEM (some shared hosts enforce limits).

Alternative: Set a High Memory Limit Instead

Instead of removing the limit, set a higher value:

ini_set('memory_limit', '512M');  // 512MB
ini_set('memory_limit', '2G');    // 2GB

Use in CLI Mode (Command Line Execution)

If you are running a PHP script via the command line, you can pass the memory limit directly:

php -d memory_limit=-1 script.php
  • Use ini_set('memory_limit', '-1'); inside PHP scripts to allow unlimited memory for that script only.
  • It is safer than modifying php.ini globally.
  • Monitor memory usage to avoid crashes.
Frequently asked questions
Per-script ini_set: one heavy endpoint among many lightweight ones — raise just for that endpoint, keep cap low elsewhere. php.ini: global raise affects all scripts. ini_set is precise; php.ini is blanket. Production with mixed workload: ini_set per heavy script.
Yes — `ini_set('memory_limit', '64M')` lowers from default. Useful if you're paranoid about specific code path leaking. Rare in practice. Typical use is raising, not lowering.
Either: directive not allowed at runtime (some directives are PHP_INI_SYSTEM only), or syntax error in value. memory_limit is PHP_INI_ALL — works at runtime. If false: check value string format ('256M' not 256 * 1024 * 1024).
If script tries to load a large file before ini_set raises the limit, OOMs first. Set ini_set at line 1, before any include/require that might allocate. For library bootstrap that allocates on import, ini_set can't help — use CLI -d flag or php.ini for those.
Related articles
Change memory_limit via ini_set() in a PHP Script (Sibling Reference)
Set PHP memory_limit = -1 via Command Line (For CLI Scripts)
How to Add a Vary: Accept Header Using PHP