tags:

views:

472

answers:

5

I'm building a webpage that queries a MySQL database and (currently) produces a text-only list of the findings. The queries to complete a single record are similar to:

  • movie (title, description, etc)
    • actors in the movie (name, gender)
    • related movies

Out of curiosity I've used memory_get_peak_usage() and memory_get_usage():

start   110,440 bytes
peak    656,056 bytes
end 637,976 bytes
time    0.008 seconds

The above is with no records found!

With 40 records I'm reaching approximately 2MB, though the time remains the same.

I know that premature optimization is evil and all, but I have to ask:

Do I need to do some reworking? Is this memory usage normal? What is considered excessive?

+5  A: 

Ultimately it's preferable to optimize when convenient of course, but 2MB of memory use sounds fine. PHP4 has a default config of 8mb, and PHP5 16mb. A lot of pre-packaged PHP builds will have different configs, of course, but generally speaking, if you can keep your app under 8mb, you can be sure it'll be highly portable in that regard.

Owen
+3  A: 

Obviously it all depends on the complexity of the application you're building. In average, the pages of our application take up to 20 MB (min) of memory. That's because there's a lot of objects in the session, and a lot of initialisation process that occur before the script can even say hello world (authentification, rights management, notifications, ...)

There's a lot of things that go in the memory space, including classes definitions, globals, function definitions, etc. Reducing the amount of loaded classes (by using auto-includes) is a good way to keep your scripts with only the necessary amount of classes definitions.

I don't know if loading up extensions changes the memory usage - that should be something to test, because by default we may not need all the default loaded extensions ?

Bertrand Gorge
A: 

As long as you plan a good caching mechanism (opcode cache, object cache, etc.), memory usage becomes less relevant. You are still way below common php framework memory usage.

Berzemus
+1  A: 

Personally I set the limit to 8 MB. If I exceed that I try to improve my application. The reason is because some shared hosts and PHP 4 configurations have the default limit of 8 MB per process.

rFactor
A: 

PHP doesn't free memory during script execution so your memory limit needs to be as high as the most memory one script will consume. If the most a script will ever consume is 2MB then set it to 2MB but if you need 780MB then set it to that.

We have large production systems with the memory limit set over 2GB. But this is obviously highly tuned for us and for our scenario, I would never advocate it for shared hosting environments.