PHP and memory

I have several questions to help me understand PHP and memory better:

  1. 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 to 64M 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 exceeds memory_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?

  1. 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?

  2. 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).

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

I’ll try to answer your questions, though I am admittedly no expert on internal matters on PHP memory .

Not directly. You can create a script that is bloated and exceeds the max memory limit and yes, your program will die. But, because PHP works as a CGI for web applications, it cleans up memory after each return of a response, so any memory that was used for a response is GCed and ready for the next request. This is also why Opcache is so important for PHP performance and has been a part of the language package since PHP 5.5.

A PHP program can also exceed the timeout limit. That could also cause a PHP application to terminate without finishing.

You’ll also need some overhead on memory for the system and PHP itself and the web server of course. For instance, if you have an 8GB machine, you should leave about 2 GB for the system overhead, PHP-FPM and the web server (assuming they are all on the same machine). The other 6GB can be used for PHP execution. If you happen to also have a database server on that machine too, you’ll need to take its memory requirements into account too.

I would say, the memory you are seeing is what PHP loads and needs for each request, at a minimum. What is exactly in the memory is unknown to me. I would venture to say, each request gets its own set of global variables, as PHP has a “shared nothing” architecture. Each request is served on its own, individually. I’d love it, if somebody who knows more could confirm or correct what I just said here.

PHP is constantly improving and I would say the differences in used memory size are a sign of such improvements. PHP 7’s memory footprint is reduced even more.

I’d say it is something like that. PHP FPM has child processes, and usually the number of children and script execution and RAM used and available determines throughput.

There is also the web server itself, which usually won’t be the bottleneck for throughput. Though, even the best system can be improperly set up for the situation at hand. Just be aware that you will also need to optimize the web server too.

I am not sure about this to be honest. I can only say, move to Nginx, if you have any memory or performance concerns. Nginx is less resource intensive than Apache.

This might explain why there is a difference in memory.


Thanks for the detailed answer, Scott. That’s very useful — especially that last link, I never knew that!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.