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

Change memory_limit via ini_set() in a PHP Script (Sibling Reference)

2 min read
21.09.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 Reference
Runtime memory_limit — `.user.ini` for per-directory; ini_set for per-script.

For the primary article and related guides, see Change memory_limit via ini_set() — primary article, php.ini Set memory_limit = -1, and CLI memory_limit = -1.

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');

?>

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).
Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

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
.user.ini affects every PHP file in the directory tree where it lives. ini_set is per-script. .user.ini is right when many scripts in one app share the same heavy needs (CMS heavy-processing area). ini_set is right for one endpoint among many. Less code change with .user.ini; less scope-creep with ini_set.
PHP-FPM caches .user.ini for `user_ini.cache_ttl` seconds (default 300). Edit .user.ini, wait 5 minutes (or restart PHP-FPM), changes take effect. In dev: set cache_ttl=0 in php.ini for instant. In production: 300s is fine, plan changes accordingly.
Yes — pool config (`php_admin_value`) is admin-level, beats .user.ini (user-level). Pool restricts what .user.ini can override via `php_admin_flag`. For host that wants to cap memory regardless of user settings: use pool's php_admin_value.
PHP-FPM hadn't reloaded. Some changes (php.ini) require full restart .user.ini changes wait for user_ini.cache_ttl. Force apply: `systemctl reload php-fpm`. Reload is safer than restart — keeps active connections alive.
Related articles
Change memory_limit via ini_set() in a PHP Script (Runtime Override)
Fixing "A Fatal Error or Timeout Occurred" by Increasing PHP Limits
Set PHP memory_limit = -1 via Command Line (For CLI Scripts)