Introduction to the PHP managed memory function memory_get_usage of

  • 2020-05-26 07:56:22
  • OfStack

Here is an example of using PHP memory_get_usage() :
 
echo memory_get_usage(), '<br />'; //143952 
$tmp = str_repeat('http://blog.micxp.com/', 4000); 
echo memory_get_usage(), '<br />'; //232048 
unset($tmp); 
echo memory_get_usage(); //143952 

The comments at the end of the program above represent their output (in units of byte (s)), which is the memory used by the PHP script at the time (not including the memory used 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 no longer need to be used. Similarly, the PHP mysql_free_result() function empties the result set of a query database that is no longer needed, thus providing more available memory.

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

So in actual programming, you can use PHP memory_get_usage() to compare the memory footprint of each method and choose which method to use with a smaller footprint.

Common detection:

The program execution time can be analyzed using the microtime function
memory_get_usage can analyze the memory footprint
The efficiency of SQL can be used to turn on slow queries to view log analysis
SQL found bottlenecks using EXPLAIN for analysis

Related articles: