Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does memory_limit work in PHP?

Tags:

php

If I have two scripts and my memory_limit option is set to 64M (for example). If I run script 1 and it takes 40M and simultaneously run script 2 how much memory does it have free?

Does every script has up to 64M or they share this memory?

like image 905
Ivanka Todorova Avatar asked Mar 09 '26 15:03

Ivanka Todorova


2 Answers

Each PHP script running independently is subject to its own memory limit (as set by the configuration option memory_limit). This limit is inherited into all included scripts (as those are dependent on the parent script).

An example - two scripts running in parallel:

  • you open a webpage /script1.php
  • you also open a webpage /script2.php

As these two are completely independent (there is no simple way to discover that "the other script" is running, or even that it exists), they will get a 64M limit each - so the amount of memory they are allowed to use is 64M for one, which comes out to 128M for two. (To clarify, it is not possible to "share" this memory between scripts: if script1.php only consumes 1MB, it can not "give" the rest of its limit to script2.php)

A different scenario - one script including another script:

  • you open a webpage /script1.php
    • that script has a line require('script2.php')

In this case, there is only a single 64MB limit: this is still considered a single script, no matter how many files it include()s or require()s. In other words, script2.php inherits the limit (as well as the other PHP settings) from script1.php and all the memory used here is counted towards that limit.

Note that it is possible to change this limit from inside the script (if your server's configuration allows this - most do). Using ini_set('memory_limit', '128M'); sets the new limit to 128 MB - but all the memory used by the script so far still counts towards this limit.

Scripts including other scripts and dynamically changing the memory limit:

  • you open the webpage /script1.php (in script1.php, limit 64 MB set from configuration)
    • that runs require('script2.php') (in script1.php, limit 64 MB inherited)
      • that runs ini_set('memory_limit','200MB') (in script2.php, changed explicitly to 200 MB)
      • that runs require('script3.php') (in script2.php, limit 200 MB as set above)
        • that runs require('script4.php') (in script3.php, limit 200 MB inherited)
          • that runs ini_set('memory_limit','-1') (in script4.php, changed explicitly to "no limit")
          • that runs require('script5.php') (in script4.php, "no limit on memory" inherited)

Note: It is possible to set a lower limit than it is currently, but this risks immediately overrunning it ("we're using 80 MB" - "set limit to 64 MB" - "out of memory error"); because of this risk, this is rarely attempted.

Note also that memory_limit is a configuration setting and as such it can be set in various places; or its modification can be prevented by the system administrator.

like image 80
Piskvor left the building Avatar answered Mar 12 '26 07:03

Piskvor left the building


From the PHP Manual:

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. Note that to have no memory limit, set this directive to -1.

Meaning each script gets the same 64M amount of memory.

like image 20
markus Avatar answered Mar 12 '26 07:03

markus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!