How to Increase PHP Memory Limit

1
3281

earlphpFatal error: Allowed memory size of xxxxxx bytes exhausted” “(tried to allocate xxxxxx) in Unknown on line x” Often times you may end up getting the previous error when running a PHP script.

This error basically means that you need to increase the memory allocated to running an individual PHP script.

While the following methods will help you to increase the PHP memory limit, you must be mindful if your hosting provider allows such methods since if you are on shared hosting this these methods may be disabled by your server provider.

[box type=”info”] If you are using PHP version 5.1.0 or above then you can use all these shorthand postfixes with the integer memory value which otherwise will be in bytes. These are K (for Kilobytes), M (for Megabytes) and G (for Gigabytes), these are case insensitive.[/box]

Inline PHP script inside .php file

This code goes in your .php file and is applied at run-time when your script is executed. So if it is in a index.php level file it will be global to the site using the index.php or if its in a specific .php file that level will be changed for that file execution.

<?php
ini_set('memory_limit', '1G'); // Increase by 1 Gigabytes

ini_set('memory_limit', '500M'); // Increase by 500 Megabytes

ini_set('memory_limit', '256000K'); // Increase by 256 Megabytes

ini_set('memory_limit', '128000000'); // Increase by 128 Megabytes
?>

Increase PHP memory limit using .htaccess

This method only applies to the current site where the .htaccess file resides in, or if its in a specific directory any .php script executed from that directory. If the .php file has inline settings that will override this setting.

php_value memory_limit 1G #Increase by 1 Gigabytes

php_value memory_limit 500M #Increase by 500 Megabytes

php_value memory_limit 256000K #Increase by 256 Megabytes

php_value memory_limit 128000000 #Increase by 128 Megabytes

Increase PHP memory limit using php.ini

Setting the memory limit in the php.ini file serves in two ways. One is that if you want to set the limit globally for all the websites on a server, then you can use this file or if you want to set the size for a specific site or script similar to the .htaccess method above, you can do that as well by placing a php.ini file in the document root of your site or the specific directory that the script if running from.

memory_limit = 1G #Increase by 1 Gigabytes

memory_limit = 500M #Increase by 500 Megabytes

memory_limit = 256000K #Increase by 256 Megabytes

memory_limit = 128000000 #Increase by 128 Megabytes

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here