Increase PHP memory limit
If you have seen an error like “Fatal Error: PHP Allowed Memory Size Exhausted” in apache logs or in your browser, this means that PHP has exhausted the maximum memory limit. This post will show 3 different ways on how you can increase the php memory limit and also explain when you should use them.
First, let’s see where is this limit coming from. Normally you will see from the error message what is the actual limit, as this will look like:
PHP Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y) in whatever.php
The default value might differ depending on what php version and linux distribution you are running, but normally this will be set to either 8M or 16M. For example on my debian etch, running on php 5.2 this is set by default at 16M.
In order to identify the current value on your system, look inside your php.ini and search for memory_limit:
memory_limit = 16M ; Maximum amount of memory a script may consume (16MB)
There are three ways to change this value, the obvious way – changing the global value from php.ini, but also an individual method to change it just for a script, or folder.
1. Changing memory_limit globally from php.ini
This is the simplest and most obvious method. You just edit your php.ini and change the memory_limit to whatever you need. For ex:
memory_limit = 32M
You will require access to make changes to php.ini on the system. This change is global and will be used by all php scripts running on the system. Once you change this value, you will need to restart the web server in order for it to become active.
Keep in mind that this limit has its logic and don’t increase it artificially, as poorly written php scripts might overkill your system without proper limits.
Note: if you know what you are doing and want to remove the memory limit, you would set this value to -1.
2. Changing memory_limit using .htaccess for a single folder/vhost
Changing the global memory_limit might not be a good idea, and you might be better changing this only inside one folder (normally one application or virtual host) that needs this value changed for its functionality. To do this you have to add to the respective location .htaccess something like:
php_value memory_limit 64M
This change will be local only, and can be useful for webmasters that don’t have control on the system php.ini. This change would not require a reload and will become active immediately.
3. Changing memory_limit inside a single php script.
For even more control you can set this directive inside a single php script. To do so you would use in your code:
ini_set('memory_limit', '64M');
The advantage of this method is that you have more control and set this value just where you know it is really needed. Also it can be done without having access to the system php.ini, and will become active immediately.
Note: in order to be able to use these PHP resource limits, your PHP version must have been compiled with the –enable-memory-limit configure option. Normally most packed versions will have this, but just in case if this doesn’t work for you as expected, check on how php was compiled first.
>
15th February 2008, 05:08
Also if your web host is running PHP as CGI(suexec) then you may save a php.ini in the web root directory ( DocumentRoot directory) or the same directory in which the script resides with,
memory_limit = 16M
17th February 2008, 22:19
[...] Increase PHP memory limit | MDLog:/sysadmin php memoria limit beallitasa minden modon. (tags: sysadmin php memory mem apache apache2) [...]
15th October 2008, 18:31
[...] c’est tout pour aujourd’hui, un grand merci à MDLog:/sysadmin. Si vous allez la moindre question posez là [...]
17th December 2008, 00:18
Posted this on Digg but I thought I’d post it here too:
Saved me big time, thanks!
I needed to figure out how to set the PHP memory size without having to edit the php.ini file as it’s inaccessible and I was not sure what the syntax was to use for that in .htaccess
11th January 2009, 08:37
I was having problem generating 20 pages of PDF report using TCPDF. Reports with only a few pages working fine. Spent a lot of time on the wrong track trying to optimum the script and query. Finally, ini_set(‘memory_limit’, ’64M’); save my headache.
Thank you very much. Now i know how PHP memory limit work
25th January 2009, 21:45
[...] jsem se do toho pustil s vervou. Vlastne stacilo zadat jeden spravny vyraz stryckovi Gogglovi a vysledek byl jasny. Nabizi se tedy tri moznosti, jak zvysit limit pameti pro [...]
4th February 2009, 18:53
Thank you. Now i know how PHP memory limit can be “unlimited”
23rd February 2009, 11:48
thanks for posting all the possible ways..
10th July 2009, 13:49
Another way is from the shell when the PHP program is launched:
% php -d memory_limit=64M -f /path/to/your/script.php
Of course, this doesn’t help if script.php is run from a browser, but many people use PHP for non-web uses.
22nd July 2009, 17:52
Good Explanation!!
23rd July 2009, 12:12
Thanks, nice article. But you don’t say *why* it should be increased or decreased, which is what I need to know. Is it something to do with number of users on a site at any one time?
Thanks
Jon
24th July 2009, 07:23
@Jon: the required memory of a php script is not dependent on the number of users or the traffic at one time. This is related to the size of the libraries included and the data the actual script will use. This is a safety measure to not have one poorly written script to overkill the system by using more resources than needed. hth.
3rd August 2009, 20:42
[...] der .htaccess Datei ebenfalls das Limit erhöhen (Punkt 2 [...]
17th September 2009, 16:40
Thank you. Clear and useful instructions on modifying PHP’s memory limit. However, you may want to update this post, as from PHP 5.2.1 onwards, the –enable-memory-limit compile-time option is no longer required to set memory limits using the methods you described. Also, from PHP 5.2.1 onwards, the default is 128 MB (given the popular use of PHP for a variety of applications).
22nd September 2009, 08:36
[...] One of our Drupal sites suddenly had this “blank page” problem after user login. So we turned on the PHP’s error reporting and got the “allowed memory size of xxx bytes exhausted” error. Usually this error is caused by memory limit for PHP so you just need to increase it from your php.ini file or .htaaccess, but in our case we had our memory limit set already to 64Mb. Check here for more details on how to increase PHP memory limit. [...]
26th June 2010, 02:06
I have a question.
Does this limit mean, each individual connection can have 16MB limit ? or overall php engine can use only 16MB of the server’s memory to serve thousands of users ???
Please reply back
26th June 2010, 02:30
Bilal: it’s the limit of any one PHP script, not the overall php engine.
26th June 2010, 06:22
so that limit will only be applied to one user connection ???
for example : 1 user = 1MB , 1000 user connections = 1000MB ???
or will it work like this:
1 User = 16 MB , 16 users = 16 / 16MB = 1MB per user connections ??
Im highly concerned about it because Im working on a file download site.
it uses curl + headers to serve files.
sometimes, it exhausts the memory of the server, Ive set the memory limit to 1024MB right now, and the server has total of 2GB memory.
I still cant understand the working of this limit. will it use 1024MB for the whole PHP engine or not ? assuming that PHP engine is running only one script.
That limit is applied to all the pages of the site , Ive added the bit in every php file.
26th June 2010, 15:53
@bilal ghouri: just like @egc52556 explained this is per script. Every script you run will have this limit not all the engine. If you have 2GB memory and allow this to be set at 1G, then 2 apache threads running at the same time can easily exhaust all your memory (this is allocated on demand, as needed, and not the full max memory; if max memory is reached the script is killed with an error in order to prevent it to overkill the system). Usually people set this reasonably to 64/128M and if a single script needs more it is overwritten inside that particular script. hth
26th June 2010, 22:28
what is meant by “each apache thread” ?? does one connection mean one thread on apache ? or what ?
27th June 2010, 00:48
One apache thread can serve many requests per second (this will actually depend on your server, configuration, etc) and it will normally die after the configured time in your apache (MaxRequestsPerChild). hth.
27th June 2010, 00:51
Actually i have an issur regarding this..
Apache isnt dying after max memory is exhausted :s
It is overkilling the server.
I have set time limit to 0 , to get maximum time out of the script.
and max memory to 1024, but the server gets overloaded, and stops respoding.. :s
whats the issue? Is it some configuration problem??
By the way, thanks for such quick responses, I really appreciate that
27th June 2010, 01:10
How long is your script running? There are various timeouts in php and apache itself that control this. As I said above each apache thread will die after serving MaxRequestsPerChild requests (as configured in main apache config).
It is hard to understand what is the problem without seeing the system, but from what you are saying that particular script sounds like the problem, that you might want to look into and optimize.
27th June 2010, 01:13
I actually tested the script on two servers.
One of them is working smoothly. (4GB ram, Core2Quad)
The other one dies when I try to download 1 file from it…with just 1 file :s (2GB ram, AMD)
27th June 2010, 01:14
the 4gb server is handles about 1000 downloads per day without any problem, but Im trying to do a cluster system.
so I bought a new server, but it dies when I test run it with 1 file..
27th June 2010, 01:14
Look into the apache logs and see what happens.
27th June 2010, 01:18
hmm okay will do that… rite now the server is down, cuz I cant even access whm or ssh on it.. i have sent my hosting company an email.. they will get it back on.
Thanks for the help
28th June 2010, 08:26
Extending memory limit in global is not a good idea IMO. And setting to something like 1G is totally crazy. Exceeding 16Meg of memory to run standard PHP script which should serve web page is indicating there is something wrong in the script itself. Most of the memory is consumed by initializing variables (not by just loading libraries or so). So code must be badly written to exceed memory for just one run.
There are specific cases though where there is need to extend memory which are pages showing long lists of data , or long running Cron scripts, or functionality where there is need for heavy recursions or running in debug mode with collecting a lot of debug outputs. But these should be increased specifically for particular scripts as needed and not just straight to crazy big value.
28th June 2010, 10:11
Its a file download script, It is SUPPOSED TO EAT RESOURCES :S
its not a simple one page serving script.
It serves files from a different server (cluster servers) using real time streaming method.
Like you clicked on a download link, the script will pick a server and take the file from that server via CURL and serve the file directly to you using partial headers.
5th July 2010, 19:15
I haven’t done stuff like this yet, but still something doesn’t seem to me right. If script is eating as much memory as file it is serving, than entire service limiting factor is number of actual connections multiplied by file size. If files are big about 1G then there is room just for few parallel downloads?
Don’t know but maybe using socket connection instead of CURL and reading source data in blocks and serving to client should still fit into standard 16M memory limit per script.
28th July 2010, 16:41
[...] [...]