PHP: How to increase “memory_limit” value using php.ini?

2013-09-17 PHP

Sometimes your scripts will consume a lot of memory. Lack of it may cause in PHP throwing a fatal error with something like this below:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 103 bytes)

The amount of bytes may be different but rest will be the same. To prevent it, you should increase value set in variable called “memory_limit”. You can do it in more than one way. The fastest method is to use below function:

ini_set()

and place it in the very first line after opening

<?php

for example:

<?php
ini_set('memory_limit', '256M');

I find this solution fast but not permanent. It will affect only script within it’s set. To create a global solution let’s try to increase memory limit in php.ini file. First we have to locate the file. Create new file and paste the following code:

<?php php_info(); ?>

Save file into root directory of your server / localhost etc. and call it for example test.php. Now in your browser type http://localhost/test.php. If the address is different, change it to a proper one. Now find the option called:

Configuration File

Value of mine contains:

/etc/php5/apache2

Open terminal, log into server and change directory to this location:

cd /etc/php5/apache2

Now use editor that you preffer and edit the file:

mcedit php.ini

Find the line that contains below phrase:

memory_limit

Change the value to a higher amount. In my case I’ve changed it from 128M to 256M. It’s good to use values that are power of number 2 (32, 64, 128, 256, 512 etc.). Save the file and exit. Final thing that you have to do is to restart you server. Mine was Apache so I just typed in terminal:

sudo service apache2 restart

After that my PHP scripts received much more memory to handle all operations correctly.

Do NOT follow this link or you will be banned from the site!