How to know the memory occupied by PHP programs _ PHP Tutorial

Source: Internet
Author: User
How to know the memory occupied by the PHP program. Do you want to know how much memory the PHP script requires? You can simply use PHP to view the memory function memory_get_usage () allocated to the PHP script. Do you want to know how much memory is required for the PHP script? You can simply use PHP to view the memory function memory_get_usage () allocated to the PHP script.

The following is an example:

 '; // 313864$tmp = str_repeat('http://www.bkjia.com/', 4000);echo memory_get_usage(), '
'; // 406048unset($tmp);echo memory_get_usage(); // 313952?>

The comments behind the above program represent their output (in bytes (s), that is, the memory used by the PHP script at that time (excluding the memory occupied by the memory_get_usage () function itself ).

The preceding example shows that to reduce memory usage, you can use the PHP unset () function to delete unnecessary variables. Similarly, the PHP mysql_free_result () function can be used to clear the result set of a database query that is no longer needed, so that more available memory can be obtained.

The PHP memory_get_usage () function can also have a parameter $ real_usage with a Boolean value. The default value is FALSE, indicating that the obtained memory usage does not include the memory occupied by this function (PHP memory manager). if it is set to TRUE, the resulting memory does not include the memory occupied by this function (PHP memory manager.

Therefore, in actual programming, you can use the memory_get_usage () function to compare the memory usage of each method and select the method that occupies a small amount of memory.

Post a usage function:

If (! Function_exists ('memory _ get_usage ') {/** + usage * get memory usage + usage * @ return integer + usage */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 ;}}}

Another example of using a function:

  m1:',$m1;//58096    $a = 'hello';  $b =  str_repeat($a,1000);    $m2 = memory_get_usage();  echo '
m2:',$m2;//63424 unset($b); $m3 = memory_get_usage(); echo '
m3:',$m3;//58456 ?>

How much memory does the ghost PHP script occupy? You can simply use PHP to view the memory function memory_get_usage () allocated to the PHP script. Below...

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.