tags:

views:

610

answers:

3
+1  Q: 

PHP memory limit

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.

+1  A: 

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.

cletus
My question is: in 5.0.4, if you don't set memory_limit, does it still enforce one?
JW
+1 , PHP's garbage collector has broken more than once. As getting rid of timeouts is so popular, its important to know what your code is actually __doing__ to a computer.
Tim Post
A: 

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.

Byron Whitlock
Do you know if that 8MB was actually enforced though? It was the default value for that setting in php.ini, but the docs say that the setting was ignored.
JW
+1  A: 

The memory limiter in PHP is optional; if you disable it at compile time there's no limit at all.

In 5.0.4 it's disabled unless you explicitly asked for it at compile time, the reason being that the memory limiter was useless until 5.2 and didn't count a lot of things it should have done, including things like the mysql functions. It's turned on from 5.2.1 now that they learned to count.

If in doubt, disable it or make sure you update the config file to use the new default. Leaving it at 8MB and upgrading to 5.2.8 will almost definitely cause problems.

Ant P.
That's the answer I was looking for -- thanks!
JW