Jump to content

Php And Memory


Kovo
 Share

Recommended Posts

HelloIm building a browser based game (text and images)I have a debugger/benchmark script that I run at every opage load which gives me various information. One thing it logs is script exectution times (including how long it took for all of the php to load and execute) as well as memory usage.Im developing on a shared host so I guess things differ a bit when I go dedicated, but I have noticed a fully loaded profile page takes around 0.05 - 1 second to execute and consumes 1.1 - 1.3 MB of memory (using phps memory_get_usage() function) Im wondering if that much memory is a bad thing or not... Imagine 100 users load a page, then imagine 10,000 users. Granted if the system had 4GB+ of memory it might be able to handle it but it seems high. I try to load just modules I need (functions, classes etc...) But even then it seems high. Any ideas? Experiences? Tips?Thanks a lot!

Link to comment
Share on other sites

It depends on what the script is doing, its hard to know the memory requirements without having an idea of the function of the script. If you are using php 5.2.0 or above then you can add real_usage as a parameter to memory_get_usage to give you an much better idea of the exact memory usage.

Link to comment
Share on other sites

It depends on what the script is doing, its hard to know the memory requirements without having an idea of the function of the script. If you are using php 5.2.0 or above then you can add real_usage as a parameter to memory_get_usage to give you an much better idea of the exact memory usage.
Its not a script more than its all scripts that run before the server sends the html to the browser.But these scripts are working to build the HTML page loading profile info from a db query or sessions, performing calculations, accessing classes, functions etc..And at the end stripping whitespace from the HTML (using ob_start with a strip function)
Link to comment
Share on other sites

You can also use memory_get_peak_usage at the end of the script to figure out the maximum it had allocated. More than 1MB of memory is a lot for just HTML processing. If you're doing a lot of database operations, it will be more efficient to make sure you only select what you need (don't select * if you only need one column), and make sure to free the database results from memory when you're finished with them. There's no need to keep things in memory if you're done using them.

Link to comment
Share on other sites

You can also use memory_get_peak_usage at the end of the script to figure out the maximum it had allocated. More than 1MB of memory is a lot for just HTML processing. If you're doing a lot of database operations, it will be more efficient to make sure you only select what you need (don't select * if you only need one column), and make sure to free the database results from memory when you're finished with them. There's no need to keep things in memory if you're done using them.
Weird thing is, I have very optimized queries, and I never use *I free my results all the time and I never get big results anyway (we are talking 1000 bytes max in most cases, sometimes more)I have a debugger class that handles all this info, Ill post an example of what it spits out:Example 1 Initial page load where I run all queries and store info in session data:68408024.jpgExample 2 second page load (same one) where we skip most queries due to session info being present31897689.jpgI dont expect you guys to understand it all, but bascialyl I load all the modules I need first then run my scripts for that particular page.MU is memory usage, and I also put memory usage and peak usage on top and bottom of the output. For memory usage the first value is the memory size and the second value is the real usage (real allocation)It seems most of the data comes from loading the modules, but the modules themselves are just full of functions or classes, and they dont go over 100KB in size total.So Im not sure why the memory usage is so high.Any ideas?edit: as you can see, my queries and subsequent result usage etc only really use up to 100KB MAX of memory. So it couldnt be my queries. Even the first image shows that a lot of queries and calculations does little to add to the memory usage. Edited by Kovo
Link to comment
Share on other sites

Its hard because I need them...lol, Browser detect could be phased out if I just did manual browser detection for the specific ones I want, but it would make future expansion messier.CSP is just 3 big functions that handle returning all the countries (so a lot of strings and if/else if statements)Functions is just a repository of all the functions I use (most commonly)Im not a completely beginner at programming and defnitly not to PHP so my code isnt long for no reason or senseless.Just boggles my mind how websites like facebook that seem to have so much data loaded dont take up as much memory as my entire page seems to need.

Link to comment
Share on other sites

Its hard because I need them...lol, Browser detect could be phased out if I just did manual browser detection for the specific ones I want, but it would make future expansion messier.
Why? The only browser I'd think you need to detect is IE. You can do that with conditional comments. You should write standards compliant pages for everyone else.
CSP is just 3 big functions that handle returning all the countries (so a lot of strings and if/else if statements)
If you have a single array, which you unset() once you find out the country, it will probably be more efficient. Your peak usage may grow slightly, due to the array's length, but its average will be lower.
Just boggles my mind how websites like facebook that seem to have so much data loaded dont take up as much memory as my entire page seems to need.
The have many servers, each being even more powerful than "simply" 4GB RAM and a single ~3GHz CPU.
Link to comment
Share on other sites

Just boggles my mind how websites like facebook that seem to have so much data loaded dont take up as much memory as my entire page seems to need.
The have many servers, each being even more powerful than "simply" 4GB RAM and a single ~3GHz CPU.
They also don't do things like load a list of every country in the world for every request. Why not use a database for that? Databases are specifically for storing your data so that it's fast and easy to look up, if you want to use local memory to store your data instead then you can expect that it's going to take a lot of memory to run your script.
Link to comment
Share on other sites

The have many servers, each being even more powerful than "simply" 4GB RAM and a single ~3GHz CPU.They also don't do things like load a list of every country in the world for every request. Why not use a database for that? Databases are specifically for storing your data so that it's fast and easy to look up, if you want to use local memory to store your data instead then you can expect that it's going to take a lot of memory to run your script.
I have 3 functions in that file.One function returns province/stateOne that will return a country name based on the passed in 2 letter country codeOne will generate a SELECT drop down with the specified country as selected by default (the specified country is a passed in variable)although it is not required on every page (and it isnt loaded on every page anyway) I do need it for various parts of the profile.
Link to comment
Share on other sites

So why not store that in a database instead of memory? Store each country in the database, and you can use that to look up a country based on the code. Same thing with provinces and states, those can be stored in the database too. If your country and state arrays are defined outside of the functions, then those will always be present in memory, even if you don't use the functions. You can test that out by having a page which only includes that one file and check the memory usage before and after including the file. In general though, it's going to be more efficient to store data in the database and load it on-demand instead of keeping everything in local memory, even though you may not use it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...