HOWTO · PHP

PHP ini_set Memory Limit

Choose a script-level or shared PHP memory limit, inspect the effective value, and verify the change.

On this page

PHP’s memory_limit setting caps the memory a script may allocate. The right value and the right place to change it depend on the application and the PHP runtime. Many installations already have enough memory for their workload, so first inspect the effective value and identify which scripts need a change. Choose the narrowest scope that solves the problem: a change in one script is local to that request, while a php.ini change can affect many scripts using that configuration. (ini_get())

Choose the right scope for the change

Use ini_set() when a single script needs a different limit and the runtime permits that setting to be changed. This keeps the adjustment close to the code that needs it. Use php.ini when the same limit should apply to scripts served by the PHP runtime that reads that file. A shared change can affect unrelated applications, so consider their memory use before raising it. Do not assume every host uses the same default or that increasing the limit is necessary.

Some hosts provide additional configuration through .htaccess or a control panel. Whether either option works depends on the server, PHP handler, and hosting policy. Treat them as host-specific alternatives and check your provider’s instructions before relying on them.

Check the current PHP memory limit

Call ini_get("memory_limit") from the same kind of PHP process that runs the affected script. It returns the value effective for that process, which may differ between command-line PHP and a web request. For a web application, temporarily display or log the value from the relevant request, then remove diagnostic output when you are done. Avoid publishing configuration details in a public page.

The returned value is a PHP configuration value, such as a size with a unit suffix. Compare it with the application’s actual needs and the memory available to the PHP process. A larger configured limit does not create physical memory or guarantee that the host can satisfy every allocation.

Raise the limit for one script with ini_set()

Pass the setting name and a nonempty size such as 256M to ini_set(). The function returns the previous value when the change is accepted, or false when PHP cannot apply it. Read the setting again afterward so the script can report the effective value rather than assuming the request succeeded.

Run this example in a PHP process:

<?php
$before = ini_get('memory_limit');
$previous = ini_set('memory_limit', '256M');
$after = ini_get('memory_limit');

echo "Before: {$before}\n";
echo 'Change accepted: ' . ($previous !== false ? 'yes' : 'no') . "\n";
echo "Effective limit: {$after}\n";
Before: -1
Change accepted: yes
Effective limit: 256M

This example reports the values observed by its own process; the starting value can vary by installation. A successful script-level change applies to the current request and does not update the shared configuration. If the return value indicates that PHP did not apply it, use the effective value for diagnosis and check the restrictions imposed by the runtime or host.

Change the shared limit in php.ini

First identify the php.ini file loaded by the PHP runtime that serves the affected scripts. PHP installations can use different configuration files for command-line and web processes, and servers may load additional configuration. Use the runtime’s configuration information or your hosting documentation to identify the active file; there is no single path that applies to every operating system and installation.

In the active file, find the memory_limit directive and set it to the intended value, for example memory_limit = 256M. Save the file and follow the PHP service or hosting provider’s instructions for making configuration changes take effect. The required action depends on how PHP is hosted, so do not assume that every environment uses the same restart procedure.

This change can apply to multiple scripts using that configuration. If only one application or request needs a higher limit, prefer an allowed script-level change or a host-supported per-site setting. .htaccess and control-panel options are not universally available; check that the specific server supports the option before using it.

Verify the effective limit and troubleshoot changes

After changing the setting, call ini_get("memory_limit") from the affected script or request and compare the result with the intended value. For a shared change, verify through the same web or command-line runtime whose configuration you edited. Checking a different PHP executable can show a different configuration and lead to a false conclusion that the change did not work.

If the value is unchanged, confirm that you edited the file loaded by that runtime, that the PHP service or host applied the change, and that the host permits the directive at that scope. For ini_set(), check its return value and then inspect the effective setting. If the value is correct but the application still runs out of memory, review its workload and actual memory use; raising the cap alone may not address the cause. Keep the limit appropriate for the resources available to the process.

References:

  1. PHP manual: ini_set()
  2. PHP manual: ini_get()
  3. PHP manual: memory_limit