How to learn how much memory a PHP program occupies (memory_get_usage) _php Tutorial

Source: Internet
Author: User
The following are examples of use:
Copy CodeThe code is as follows:
Echo memory_get_usage (), '
'; 313864
$tmp = str_repeat (' http://www.nowamagic.net/', 4000);
Echo memory_get_usage (), '
'; 406048
Unset ($TMP);
Echo Memory_get_usage (); 313952
?>

The comments that follow the above program represent their output in byte (s), which is the memory used by the PHP script (which does not contain the memory occupied by the Memory_get_usage () function itself).

As you can see from the above example, to reduce the memory footprint, you can use the PHP unset () function to remove variables that you no longer need to use. Similar to this: the PHP mysql_free_result () function, which empties the result set from the query database that is no longer needed, can also get more usable memory.

The PHP memory_get_usage () function can also have a parameter, $real _usage, whose value is a Boolean value. The default is FALSE, which indicates that the resulting memory usage does not include the memory used by the function (PHP memory manager), and when set to TRUE, the resulting memory is not included in the memory occupied by the function (PHP memory manager).

So in the actual programming, you can use the Memory_get_usage () function to compare the amount of memory used by each method, to choose the use of a small memory-consuming method.

Paste a usage function:
Copy CodeThe code is as follows:
if (!function_exists (' memory_get_usage '))
{
/**
+----------------------------------------------------------
* Get Memory usage
+----------------------------------------------------------
* @return Integer
+----------------------------------------------------------
*/
function Memory_get_usage ()
{
$pid = Getmypid ();
if (Is_win)
{
EXEC (' tasklist/fi ' PID eq '). $pid. '/fo LIST ', $output);
Return preg_replace ('/[^0-9]/', ', $output [5]) * 1024;
}
Else
{
EXEC ("Ps-eo%mem,rss,pid | grep $pid ", $output);
$output = Explode ("", $output [0]);
return $output [1] * 1024;
}
}
}

Here's another example of function use:
Copy CodeThe code is as follows:
Memory_get_usage ();
$m 1 = memory_get_usage ();
Echo '
M1: ', $m 1;//58096
$a = ' Hello ';
$b = Str_repeat ($a, 1000);
$m 2 = Memory_get_usage ();
Echo '
M2: ', $m 2;//63424
Unset ($b);
$m 3 = Memory_get_usage ();
Echo '
M3: ', $m 3;//58456
?>

http://www.bkjia.com/PHPjc/325945.html www.bkjia.com true http://www.bkjia.com/PHPjc/325945.html techarticle Here is the example of Use: Copy code code as follows:? PHP echo Memory_get_usage (), ' br/';//313864 $tmp = str_repeat (' http://www.nowamagic.net/', 400 0); Echo memory_get_usage (), ' B ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.