Memory limit php ini

PHP sets a limit on how much memory PHP scripts are allowed to use so that a memory-intensive script won't bring down a system. PHP will exit and throw the following error once the limit is reached;

PHP Fatal error: Allowed memory size of xxxx bytes exhausted (tried to allocate yyyy) in yourscript.php

Memory limit php ini

You can increase the memory limit if your PHP scripts require to avoid the error. Some methods to increase the memory limit in PHP are configuring php.ini or .htaccess file and using ini_set() function. cPanel provides the option to configure PHP memory limit from the web interface.

Methods to increase PHP memory limit:

Increase PHP memory limit via file (php.ini)

This method will apply to all the PHP scripts that run in the system. It is a good method to follow if you're not hosting multiple systems in a single host.

  1. Open PHP configuration file using your preferred text editor.

    $ sudo vi /etc/php/7.2/apache2/php.ini

  2. Search for memory_limit directive within the configuration file.

    ; Maximum amount of memory a script may consume (128MB)
    ; http://php.net/memory-limit
    memory_limit = 128MB

  3. Set the memory limit value with appropriate unit.

    ; Maximum amount of memory a script may consume (128MB)
    ; http://php.net/memory-limit
    memory_limit = 512MB

  4. Restart your web server for the changes to take effect.

Increase PHP memory limit via .htaccess

To make the setting effective only to a certain folder (or project), you can use the .htaccess file to do the trick. To do this, locate (or create) .htaccess file in your PHP scripts folder and add the following line;

php_value memory_limit 512MB

Increase PHP memory limit via ini_set() function

It is probably the best method as the setting applies only to the particular script and would not allow other poorly written scripts to consume and waste the system's memory.

To do this, add the following line in your PHP script;

ini_set('memory_limit', '512MB');

Increase PHP memory limit for cPanel

You don't have access to PHP's configuration file if you're hosting your system in cPanel, but cPanel do provide the option to change the setting from its admin dashboard.

Memory limit php ini

Discuss the article:

Comment anonymously. Login not required.

Last updated on:  2020-10-18

Authored by:  James Andrade


The PHP memory_limit is the maximum amount of server memory that each PHP script is allowed to consume.

Per the PHP documentation:

“This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts from eating up all available memory on a server.”

The default value is 128MB. Often, this is raised depending on the amount of memory needed for the web application.

When the PHP script exceeds the limit, you will receive an error similar to the following example:

Fatal error: Allowed memory size of x bytes exhausted (tried to allocate x bytes) in /example/php/script
PHP Fatal error: Out of memory (allocated x) (tried to allocate x bytes) in /example/php/script

You can increase the memory_limit by editing the php.ini file. You should look for a line cthat starts with memory_limit:

memory_limit = 256M

Be aware that the server has a physical memory limit. You should optimize your code if the memory_limit parameter is already set too high.

©2020 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

Memory limit php ini

See license specifics and DISCLAIMER

How to Increase PHP Memory Limits

Warning! Erroneous entries to this file can crash your website.

  1. Log in to your account Hosting Summary

    Memory limit php ini

  2. On the left pane, click Hosting Summary, Scripting Config. link.

    Memory limit php ini

  3. From the Scripting Config. page, click on Manage PHP Scripting.

    Memory limit php ini

  4. Click on Manage php.ini.

    Memory limit php ini

  5. In the php.ini file, locate memory_limit=

    Memory limit php ini

  6. Make your revision and click on Save.

    Memory limit php ini

  7. A popup message will appear that the change has been saved.

    Memory limit php ini

Important: Shared hosting accounts are limited to no more than 64M, even if the limit is set to be higher inside of the php.ini file. VPS or Dedicated hosting services can set the limit to match their RAM. To help your website run at maximum performance, it is optimal for the memory limit to be set as low as possible while still allowing your website to function.