(PHP 5 >= 5.2.0)
memory_get_peak_usage — Returns the peak of memory allocated by PHP
Returns the peak of memory, in bytes, that's been allocated to your PHP script.
Set this to TRUE to get the real size of memory allocated from system. If not set or FALSE only the memory used by emalloc() is reported.
Returns the memory peak in bytes.
Version | Beschreibung |
---|---|
5.2.1 | Compiling with --enable-memory-limit is no longer required for this function to exist. |
5.2.0 | real_usage was added. |
It should be noted that this function is more accurate for measuring the total memory usage of a PHP script than memory_get_usage(), which will instead only return the amount of memory being used at the time it was invoked.
If this difference is still unclear just remember that as a PHP script builds its rendered version it uses varying amounts of memory per microsecond. Retrieving the peak memory usage will simply tell you that at one point the script required the use of "this much" memory in the microseconds that it took to build the page, which is certainly the more accurate number if your intention is to determine how much memory a script uses. It's useful to know this particularly when working in a shared environment with limited resources.
Furthermore, most standard installs of PHP limit memory usage per script to 8MB or 16MB in some cases; hosts tend to enforce such defaults. If you note the peak memory usage of your script is 7MB don't immediately worry or exacerbate the worry by doing a superficial calculation to tally how much memory the given page will consume for 1000 visitors, for example. Remember this very important fact: such peak script memory consumption is on the level of microseconds. The only way that particular script will require a dedicated 7000MB of memory, given our example, is if all 1000 visitors visited the page at the very same microsecond.
Is 8MB a lot for a script to consume? Is your page too expensive resource-wise if it does require a max of 8MB at one point? To answer either: not necessarily. An 8MB default was established precisely because it has been defined as a somewhat reasonable ceiling. My intent in stating this is to alleviate any benchmark or performance woes one may experience, particularly those who are new to the language. For example, if you're doing a simple database query or looping through relatively small arrays of data and you notice the script memory consumption has reached that ceiling, then, and only then, should you be alarmed; however, if you're performing multiple database queries with complex table joins or looping through huge arrays of data, then expect the peak memory consumption to be high, which in this case is reasonable. There's no absolute answer for determining unreasonable or average script peak memory consumption, but there are answers relative to the function or purpose of a given script.
A good article on the two functions mentioned is located at http://www.ibm.com/developerworks/opensource/library/os-php-v521/
Good day!