Reputation: 115
Here is my code:
echo memory_get_usage() . "\n";
// My PHP code is here.
echo memory_get_usage() . "\n";
The first line always echos 125800 and second line echos something variable around 129448. So, does this mean all my PHP scripts will use 125800 Bytes of RAM at the least and depending on the demand it can go up as needed?
**Update - **
I read this question. Here is a quote from accepted answer
When looking at your memory usage, you must look at the memory that is being used by the interpreter as well. Look at the big picture. The number being given to you by memory_get_usage() is the correct number.
My question is if the same script is run multiple times does the memory allocated for each script is 129448 Bytes or does the interpreter takes just one time memory of 125800 and more instances of the script run on 129448 - 125800 Bytes of memory.
Upvotes: 1
Views: 787
Reputation: 779
I guess you are concerned about a script's memory usage in a web server environment.
Depending on your web server and php configuration, the web server may spawn a separate process with an instance of the interpreter for each request or spawn threads that use a common instance of the interpreter. For Apache httpd see this. Also, for php see the 4th paragraph here (the SAPI and CGI part).
So, the total memory allocated depends on your configuration.
Other than that I guess you can get a good estimation of your php script's memory usage by doing:
$initial_mem = memory_get_usage();
// php code
echo memory_get_usage()-$initial_mem." bytes";
This doesn't take account of the interpreter's allocated memory.
To answer your question if you run a script n
times then the interpreter will run k
times where k <= n
. That k
depends on your configuration. That means you will totaly use k * interpreter_memory + n * scipt_memory
.
If you are satisfied by your script's memory consumption but you want to achieve overall lower memory consumption then I suggest to monitor the total memory usage of all the php instances in your server (use top in linux, or Task Manager in Windows or something like that) while generating high traffic by using a tool like Apache ab or Apache JMeter. Then try different configurations to achieve a better overall performance.
Upvotes: 1