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:
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?
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.
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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With