≡ Menu

How to check and increase the PHP memory limit?

The question – I need to check what is the memory limit that is set for my PHP scripts. I also get an error saying allowed memory limit of nnnn bytes exhausted. How do I check and increase the PHP memory limit?

There are several ways in which you can check your PHP memory limit. One is to view the file php.ini, another is to use the interpreter executable php.exe and lastly, you can use the php function phpinfo().

And there are two ways in which you can increase the memory limit. One is by changing the globally used value in php.ini and the other is to use the ini_set command inside of your PHP script.

Note that this memory limit is not the memory limit for the PHP process. It is the limit of memory that each individual PHP script can run in. So if you set the memory_limit to 128 mega bytes, each individual PHP script can use a maximum of 128 mega bytes.

1) How to check the memory limit imposed by PHP?

There are several ways in which you can check your memory limit.

a) Using php.exe

Run the following command on your SSH shell or local terminal:

php -i | grep memory

This will output something like the following:

root@server [/etc]# php -i | grep memory
memory_limit => 32M => 32M
root@server [/etc]#

So the memory_limit is 32 mega bytes.

b) Using php.ini

First locate your php.ini file:

root@server [/etc]# php -i | grep php.ini
Configuration File (php.ini) Path => /usr/local/lib
Loaded Configuration File => /usr/local/lib/php.ini
root@server [/etc]#

Now run the following command:

root@server [/etc]# grep -i memory_limit /usr/local/lib/php.ini
memory_limit = 32M
root@server [/etc]#

c) Using phpinfo()

Create a file called phpinfo.php. Add the following PHP code to it:

phpinfo (); 

Now upload the file to your web server and access it using http://www.yourdomain.com/phpinfo.php. Then search for memory_limit. This will show you something like the following image:

PHP Memory Limit phpinfo

2) How to increase the memory limit in PHP?

There are two ways in which you can increase or decrease the memory_limit variable. One is to change the globally used memory_limit variable using php.ini. The other way is to set the memory_limit variable in each individual PHP script.

a) Change php.ini

The value of the memory_limit variable in php.ini is applicable to each and every PHP script on your system (unless that PHP script overrides the value using ini_set).

To change the value of the memory_limit variable, open your php.ini file in an editor:

vi /usr/local/lib/php.ini

Now find the line that begins with memory_limit. Change that line to something like the following:

memory_limit = 128M

This will set your default maximum memory usage to 128 mega bytes! This will apply to all PHP scripts that run using this php.ini initialization variables.

b) Using ini_set

Each individual PHP script can override the initialization variables set in the file php.ini. This is done using the PHP command ini_set. The ini variables that are set using ini_set apply to this PHP script only.

To set the memory_limit to 512 mega bytes, add the following line to the beginning of your PHP script:

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

You can also choose to make the memory limit to unlimited by using the following PHP code at the beginning of your PHP script:

ini_set('memory_limit', '-1');

These values (512M and -1) will override the settings in php.ini for the script in which they are defined.

That’s it! You have now mastered the different ways in which you can play with the memory limit of PHP! Try them and leave your comments!

Incoming search terms:

  • centos php memory limit not working (1)

Comments on this entry are closed.