I have several questions to help me understand PHP and memory better:
-
Let’s say you’re on VPS hosting and you have a memory limit of 64M and, for whatever reason, you have set
memory_limit
to64M
so it can use the maximum. When you debug a script and get the allocated memory does this include the PHP software itself (the interpreter) or just what the interpreter uses? Do you only ever run into scripts dying if the allocated memory exceedsmemory_limit
? I.e. you don’t need to worry about the memory footprint of PHP itself. If I run a script with just this on:Memory used: <?= round((memory_get_usage(false) / 1024 / 1024), 2) ?> MB
Peak memory: <?= round((memory_get_peak_usage(false) / 1024 / 1024), 2) ?> MB
I get 0.2MB under PHP 5.4 and 5.5 and 0.6MB under 5.3. What accounts for this memory and why does 5.3 use so much more? Is it PHP’s global variables, etc?
-
If you have, for argument’s sake 100M available RAM, your scripts take 0.1 seconds to execute and use 10M each, can you assume that your site can handle 100 requests a second? Or is it not that simple?
-
If a server runs of of memory while there are other requests coming in, what happens? Does it reject the connects or queue them? I guess this is more down to the web server (I use Apache).
-
Take this code:
$object = new Class();
$objects = array();// Method 1
for($counter = 1; $counter <= 100000; $counter ++) {
$objects = new Class();
}// Method 2
for($counter = 1; $counter <= 100000; $counter ++) {
$objects = new $object;
}
If I run just method 1 it uses up three times the amount of memory than just method 2. Why is this? Presumably since all 100,000 objects can be altered independently, i.e. they’re not static, they should use the same amount of memory.