Use PHP function memory_get_usage to get the current PHP memory consumption to achieve program performance optimization

Source: Internet
Author: User
PHP performance Optimization process to avoid the need to get PHP memory consumption, using the Memory_get_usage () function to get the current memory consumption situation, the function is simple to use, the following example of the form of the use of Memory_get_usage () function to get the current usage of PHP memory consumption.

One, function prototype int memory_get_usage ([bool $real _usage=false])

Two, version compatible PHP 4 >= 4.3.2,php 5

Third, basic usage and Example 1, get current memory consumption

Echo Memory_get_usage ();
$var =str_repeat ("www.scutephp.com", 10000);
Echo Memory_get_usage ();
Unset ($var);
Echo Memory_get_usage ();

Result output: 62328 122504 62416

Description: The Memory_get_usage () function outputs a value of bytes Unit 2, and the formatted Memory_get_usage () result is output in kilobytes


$unit =array (' B ', ' KB ', ' MB ', ' GB ', ' TB ', ' PB ');
Return @round ($size/pow (1024x768, ($i =floor (log ($size, 1024))), 2). ' '. $unit [$i];
}
echo Convert (Memory_get_usage (true));

?>

Output: Kb3, custom function get array or variable value size

function Array_size ($arr) {
Ob_start ();
Print_r ($arr);
$mem =ob_get_contents ();
Ob_end_clean ();
$mem =preg_replace ("/\n +/", "", $mem);
$mem =strlen ($MEM);
return $mem;

$memEstimate =array_size ($GLOBALS);

?>

As you can see, to reduce the memory footprint, you can use the PHP unset () function to remove variables that you no longer need to use.

Similar to the following:

The PHP mysql_free_result () function allows you to empty the resulting set of query databases that you no longer need, so you can also get more usable memory.

PHP Memory_get_usage () 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 included in the memory used by the function (PHP memory manager). So in the actual programming, you can use PHP memory_get_usage () to compare the various methods to occupy the high and low memory, to choose the use of a small footprint method.

  • 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.