Forum Moderators: coopster

Message Too Old, No Replies

memory get usage()

         

cantona

10:35 am on Sep 9, 2009 (gmt 0)

10+ Year Member



Hi

This is probably a silly question.... but I have a file that uses a class. If I echo memory_get_usage() in the php file, will it just display the memory used by the file or by the file AND class combined?

Currently my php file is showing a total of 16MB used memory. But if my memory limit is set to 20MB, it breaks as if I have gone above this limit. In fact the only way to run it would be to increase it to 200MB!

I have another thread where I'm investigating other ways to reduce the memory usage of the file and class, but I am curious as to why my memory usage is showing one thing, where in reality it's something different....

CyBerAliEn

4:40 pm on Sep 9, 2009 (gmt 0)

10+ Year Member



My understanding is that the PHP functions are not 100% accurate.

However... the PHP function you note will only return the memory used by the script AT THE TIME IT IS CALLED. You can get the peak memory used (from beginning of script until this function is called) with the function memory_get_peak_usage().

It should report the memory used by the executing script... which should include everything: any included files, functions, classes, methods, variables, etc.

My guess is... your script is reporting "16mb" used when the function is called, but that after the function is called your script hits your ceiling (20mb) and breaks. This is because PHP will use up AND release memory AS IT PROCESSES. So at some point your script might be 16mb, but with one more function call/database query/loop/etc, it might "temporarily" exceed your ceiling.

To get an "accurate" measure of memory usage, I would suggest calling EACH of the functions (memory usage and peak usage) throughout your script (for debugging; do not do this in production). This should give you an idea of exactly where your "bottle neck" is actually occurring. In other words, call these functions at strategic points in the script and echo the results; and also include them at the beginning/ends of loops. This will give you a decent idea of memory utilization through out your script; and to the best of my knowledge, there is no other good way to do it (though having direct server access would probably open some options).

cantona

10:48 am on Sep 10, 2009 (gmt 0)

10+ Year Member



Thats for the great reply. I checked out memory_get_peak_usage() and used it. It's still looking wrong to me though. As I'm displaying it at the end of my file (just before phplib parses) and it's saying the peak is 35MB. So it must use more during the parse process, but I'm not sure how to debug that. I can't see the parse process taking over 100MB though... hhmhmmmm.