Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP memory limit

Tags:

php

In PHP 5.0.4, if you don't configure -enable-memory-limit, the memory_limit directive is ignored. (It's set to 8M in the recommended php.ini file, but the documentation says it's ignored.) So in that case, is there a per-script memory limit at all, or is it only limited by the system?

I ask because I'm upgrading to PHP 5.2.8, and it does allow memory limiting by default. So now I actually have to set the value to something appropriate. The recommended php.ini file now has it set to 128M, but I don't know if that's more or less than what 5.0.4 did by default!

I'm upgrading production systems, so I'd like to avoid any major change in behavior. The documentation (search for "memory_limit") is very confusing on this point. It says "default", but I don't know if that means the default value set in the config file, or the default value that it uses when memory limiting is disabled.

like image 957
JW. Avatar asked Mar 06 '09 22:03

JW.


People also ask

What should PHP memory limit be set to?

A typical appropriate memory limit for PHP running Drupal is 128MB per process; for sites with a lot of contributed modules or with high-memory pages, 256MB or even 512MB may be more appropriate. It's often the case that the admin section of a site, or a particular page, uses much more memory than other pages.

Where can I see PHP memory limit?

By default, a PHP script can allocate up to 128 megabytes of memory. To verify the current value of the memory_limit directive and other directives, you can use the phpinfo() function.

How do I give PHP more memory?

To increase the PHP memory limit setting, edit your PHP. ini file. Increase the default value (example: Maximum amount of memory a script may consume = 128MB) of the PHP memory limit line in php.

How much memory does each PHP process consume?

Memory Limit is Not The Same as RAM While RAM is the total available memory, the memory limit is per PHP process. That means that your site can consume e.g 10 GB of RAM, with a memory limit of 256 MB.


2 Answers

128M is very high. You may need that but I'd be surprised.

More to the point, the limit can be set to a global default in php.ini:

memory_limit = 32M

You can also override it in scripts:

<?php
ini_set('memory_limit', '128M');
...

You'll probably find you only have a handful of scripts that need a lot of memory. Find some comfortable value (with testing) and then just up it for the ones that need more.

like image 58
cletus Avatar answered Oct 14 '22 14:10

cletus


The default memory limit in php before 5.2 was 8MB, it was increased to a default of 16MB in php 5.2.0. It is currently a default of 128MB.

To reproduce behavior of the pre 5.2 versions, explicitly set the memory limit to 8MB.

Look under "Resource Limits" on the php.net website.

http://us.php.net/ini.core

EDIT

"Prior to PHP 5.2.1, in order to use this directive it had to be enabled at compile time by using -enable-memory-limit in the configure line. "

Check the compile flags of your old server, if you didn't have it enabled no limit was enforced.

like image 43
Byron Whitlock Avatar answered Oct 14 '22 14:10

Byron Whitlock