Default memory limit in 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.

asked Mar 6, 2009 at 22:04

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.

answered Mar 7, 2009 at 23:58

0

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.

Darryl Hein

140k89 gold badges213 silver badges259 bronze badges

answered Mar 6, 2009 at 22:08

cletuscletus

604k162 gold badges901 silver badges939 bronze badges

2

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.

answered Mar 6, 2009 at 22:13

Byron WhitlockByron Whitlock

51.7k28 gold badges119 silver badges167 bronze badges

1

Not the answer you're looking for? Browse other questions tagged php or ask your own question.

PHP memory_limit is per-script, just as a highway’s speed limit is per-vehicle. For example, although PHP’s memory limit may be set high to 1GB, that does not mean that scripts will pile up to use that 1GB. Let’s take a quick look at understanding PHP’s memory_limit setting.

PHP memory_limit is a per-script setting

PHP.net’s documentations puts it this way:

This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server...

Source: http://php.net/manual/en/ini.core.php#ini.memory-limit

Unlike say, MySQL’s key_buffer_size or innodb_buffer_pool settings, PHP memory_limit setting is not a storage space where multiple PHP scripts pool from or grow within. Rather it’s a per-script limit. PHP memory_limit is the maximum amount of server memory a single PHP script is allowed to consume. When blocked, the resulting error output looks something like this:

Fatal error: Allowed memory size of x bytes exhausted (tried to allocate x bytes) in /path/to/php/script

or like this:

PHP Fatal error: Out of memory (allocated x) (tried to allocate x bytes) in /path/to/php/script

So, for example, if two or more scripts are requested simultaneously, each is completely independent of the other. They do not share the memory_limit setting. Remember, PHP is not designed for and does not support multithreading. Thus, if five (5) PHP scripts are simultaneously using 100MB each, that would total 500MB of PHP memory usage, and a PHP memory limit of 128M wouldn’t be hit.

That said, for scripts that request other PHP scripts inline using require, include, or include_once, this limit is then inherited and shared by all included scripts that are dependent on the parent script.

“The include statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.” – W3schools.

“The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.” – php.net

Default memory limit in php

PHP memory_limit is per-script, just as a highway’s speed limit is per-vehicle.

Now regarding the original example mentioned at the outset. A lower setting of 128M is always better because if PHP scripts are trying to use more than 128M, those scripts would now return memory limit exceeded errors. In the above issue, that was not the case, so regardless of 128M or 1G memory_limit setting, it only comes into play if there’s inefficient script(s).

Fortunately, the PHP memory_limit setting will block inefficient code, which then alerts you to optimize your code. Until fixed, you may want to temporarily increase PHP memory_limit to avoid your web application becoming unusable due to PHP out-of-memory errors.

If you don’t have available memory on your server, you’ll sometimes be faced with deciding whether to increase PHP memory_limit to meet the requirements of scripts or to optimize your code. It would be best if you always optimized as the preferred option when possible. Also, you can increase PHP’s memory limit for specific websites. One method would be to place a php.ini file in the site’s webroot. You can even set the limit for specific scriptname.php. For example using ini_set(‘memory_limit’,’256MB’).

How to increase PHP memory_limit

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.ini.

memory_limit = 256M

Alternatively, you can edit your .htaccess file (Not recommended. See: Apache Performance: Disable .htaccess)

php_value memory_limit 256M

If you don’t have access to these files or lack the experience to make this change, you can contact your web host and ask them to increase your PHP memory limit.

Originally published: Sep 18th, 2017.
Last updated: August 19th 2021

Tags: memory, performance, php

How do I check my PHP memory limit?

As long as your array $phpinfo['PHP Core']['memory_limit'] contains the value of memory_limit , it does work the following:.
The last character of that value can signal the shorthand notation. ... .
The beginning of the string is converted to a number in PHP's own specific way: Whitespace ignored etc..

How does PHP memory limit work?

The PHP memory limit is the maximum server memory each PHP script can extend. Per the PHP documentation: “This sets the memory in bytes that a script is allowed to designate. This makes a difference in preventing ineffectively composed scripts from eating up all accessible memory on a server.”

What is the maximum amount of memory that a PHP script executed by Apache?

By default, a PHP script can allocate up to 128 megabytes of memory. You can modify this limit by changing the memory_limit directive in an . htaccess file.

What is WP memory limit?

WordPress has a 32MB memory limit by default, but some hosting providers increase this to 64MB for all customers. It's also possible to go even bigger — all the way up to 256MB.