For 6 months now .. I've been working on a really big PHP application suported by other Java applications.. lets just say my PHP application handles the web distribution and the administration of the whole sistem..
but .. I was developing on my home machine .. winXP athlon 64, apache 2 and php 5.. I uploaded the application and started testing on a main server .. 4 Gigs of ram .. a disk array .. apache 2 and php5 .. and ran into a big problem ...
scripts consumed more than 8 Megs of ram ... on my comp the same script ran on default PHP.ini settings and on the big sistem I had to change to 32 megs for script to work ..
anybody ran into the same problem ..
btw .. the database was the same .. the data from the databse was the same .. everything was the same from the data point of view .. I just cant figure out why 4 times more memory usage !?
Have you considered that perhaps memory limits were disabled on your Windows development machine? (e.g. it wasn't compiled with --enable-memory-limit for whichever reason) - but on the production machine it was.
If memory limits are enabled - it'll tell you how much memory you're currently using, if not - it'll probably throw up an error.
To be honest 32mb really does sound like quite a lot per-client, which I guess could cause problems with scalability (lets not go into that here).
If you're using something like Zend Studio or another PHP IDE with good debugging integration, you should be able to setup a watch that gets the value of memory_get_usage() - so while you're stepping through you can see memory usage in near real-time.
Unless you compile your own php binaries on windows, you won't have the memory-limit enabled. I don't know why, but that's the way it goes. I was actively searching for something like that and couldn't find anything, I ended up installing Ubuntu (which I am very happy with).
Anyway, if you want to work on your memory problems, you should develop or test on a machine that has php binaries with memory-limit enabled. Then you can use the xdebug extension to check what parts of the code are responsible for the most memory usage.
Typical cases will be:
- a very large codebase. If you are including very many files with several thousands LOC, this will consume quiet a bit of memory. Solution: See if you can split up your files (especially templates, language files etc.) and only include what you really need. You can also install an opcode cache, however that will only help w/ respect to scalability, because initially the scripts have to be loaded and processed at least once.
- reading in lots of data from the database, and storing it in variables, that will later get processed. Solution: Process the data row by row, and store only that which you reall need.
- first creating lots of (large) objects, then rendering their output. Solution: If possible, let each object be rendered right after creation, and then unset() the variable, before going to the next one.
- Using many output buffers, if the default buffer size is pretty large. Solution: Use a smaller buffer size, if possible. Sometimes the generated pages themselves are very large: If you show a list of several hundred items with many nested table, the generated page itself may weigh more than a Megabyte. THen you should break up the page (for the sake of usability that's a good idea) into several smaller ones, and let the user page between them.
The other option is of course to just change the php configuration, if you are not expecting many concurrent users, this won't be a problem. And adding RAM may be the cheaper option...
10x for advice .. and thank you for telling me that the WIN php does not support memory limits .. its a good idea .. but since this is a framework I'm working on .. it would be really foobared if I had to change everything for cosuming less space .. but anyhow .. thanks for the tips appreciate it !!!