Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does apache PHP memory usage really work? [closed]

To give some context:

I had a discussion with a colleague recently about the use of Autoloaders in PHP. I was arguing in favour of them, him against.

My point of view is that Autoloaders can help you minimise manual source dependency which in turn can help you reduce the amount of memory consumed when including lots of large files that you may not need.

His response was that including files that you do not need is not a big problem because after a file has been included once it is kept in memory by the Apache child process and this portion of memory will be available for subsequent requests. He argues that you should not be concerned about the amount of included files because soon enough they will all be loaded into memory and used on-demand from memory. Therefore memory is less of an issue and the overhead of trying to find the file you need on the filesystem is much more of a concern.

He's a smart guy and tends to know what he's talking about. However, I always thought that the memory used by Apache and PHP was specific to that particular request being handled. Each request is assigned an amount of memory equal to memory_limit PHP option and any source compilation and processing is only valid for the life of the request.

Even with op-code caches such as APC, I thought that the individual request still needs to load up each file in it's own portion of memory and that APC is just a shortcut to having it pre-compiled for the responding process.

I've been searching for some documentation on this but haven't managed to find anything so far. I would really appreciate it if someone can point me to any useful documentation on this topic.

UPDATE:

Just to clarify, the autoloader discussion part was more of a context :).

It may not have been clear but my main question is about whether Apache will pool together its resources to respond to multiple requests (especially memory used by included files), or whether each request will need to retrieve the code required to satisfy the execution path in isolation from other requests handled from the same process.

e.g.: Files 1, 2, 3 and 4 are an equal size of 100KB each. Request A includes file 1, 2 and 3. Request B includes file 1, 2, 3 and 4.

In his mind he's thinking that Request A will consume 300KB for the entirety of it's execution and Request B will only consume a further 100KB because files 1,2 and 3 are already in memory.

In my mind it's 300KB and 400KB because they are both being processed independently (if by the same process).

This brings him back to his argument that "just include the lot 'cos you'll use it anyway" as opposed to my "only include what you need to keep the request size down".

This is fairly fundamental to how I approach building a PHP website, so I would be keen to know if I'm off the mark here.

I've also always been of the belief that for large-scale website memory is the most precious resource and more of a concern than file-system checks for an autoloader that are probably cached by the kernel anyway.

You're right though, it's time to benchmark!

like image 237
Sirhara Avatar asked Jul 13 '12 13:07

Sirhara


1 Answers

Here's how you win arguments: run realistic benchmark, and be on the right side of the numbers.

I've had this same discussion, so I tried an experiment. Using APC, I tried a Kohana app with a single monolithic include (containing all of Kohana) as well as with the standard autoloader. The final result was that the single include was faster at a statistically irrelevant rate (less than 1%) but used slightly more memory (according to PHP's memory functions). Running the test without APC (or XCache, etc) is pointless, so I didn't bother.

So my conclusion was to continue use autoloading because it's much simpler to use. Try the same thing with your app and show your friend the results.

Now you don't need to guess.

Disclaimer: I wasn't using Apache. I cannot emphasize enough to run your own benchmarks on your own hardware on your own app. Don't trust that my experience will be yours.

like image 58
Matthew Avatar answered Oct 06 '22 01:10

Matthew