How to Increase PHP Memory Limit

In order to maintain stability and performance, web hosting companies have to put a limit on the amount of memory a PHP script can use. Usually this limit is set to 32MB or 64MB.

And when your PHP script requires more memory than allowed, you will get an error message similar to this one :

Fatal error: Allowed memory size of 12582912 bytes exhausted (tried to allocate 23456789 bytes) in somefile.php

Fatal Error: PHP Allowed Memory Size Exhausted

When this issue arise, there are multiple ways to increase the memory limit. This is assuming your web host allows you to override their global PHP configuration.

How to Find Out the Memory Limit Value

Create a PHP file named phpinfo.php with the following content :

<? phpinfo(); ?>

Save the file and upload it to the root folder of your website. Using your web browser, go to http://www.yourwebsite.com/phpinfo.php

Find the parameter named “memory limit” to see its value in the next column.

Increasing PHP Memory Limit Using ini_set()

This method isn’t recommended if you need to increase the memory limit for a large website running PHP (like a blog or forum) since it requires the modification of each and every page.

To increase the memory limit, simply add this line at the beginning of your PHP script :

<? ini_set(‘memory_limit’, ’96M’); ?>

However keep in mind this method may not give desired result on a complex PHP website.

Increasing PHP Memory Limit Using .htaccess

This method is probably the best since it will increase the memory limit for all the PHP files contained within a directory and its sub-directories.

Create a flat file named “.htaccess” (without the quotes) and insert the following line at the beginning :

php_value memory_limit 96M

Save the file and upload it to the root folder of your website using FTP. If your web host doesn’t allow this method, your website will throw a 500 error. If this happens, simply revert the change to your .htaccess file.

Increasing PHP Memory Using php.ini

This method involves the creation of PHP creation file which will be appended to the server’s default configuration.

Create a flat file and insert the following line :

memory limit = 96M

Save this file and upload it to your web server using FTP. The downside of using a php.ini file is that you will need to upload a copy in each and every directory containing PHP files.

Be Careful!

In the examples shown above, the memory limit would be set to 96MB. There is no use to setting a higher memory limit than actually needed. This could only lead to your account being suspended for using too much resource.

In doubt, contact your web hosting provider to discuss the best solution for your website.

0 responses so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment