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.
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');
?>
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');
?>
echo "Current memory limit: " . ini_get('memory_limit');
?>
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
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.


