This article describes the operation of querying memory information by php. Share it for your reference, as follows:
PHP query memory information to better view memory usage and better optimize code.
Check the current memory usage:memory_get_usage()
function.
Check the memory usage peak:memory_get_peak_usage()
function.
<?php header("Content-Type:text/html;charset=utf-8"); /** * Format byte size * @param number $size Number of bytes * @param string $delimiter Number and unit separator * @return string The size of the formatted unit */ function format_bytes($size, $delimiter = '') { $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024; return round($size, 2) . $delimiter ."&nbsp;".$units[$i]; } echo "Initial Memory Status:".format_bytes(memory_get_usage()); echo "<hr/>"; echo "Start with memory<br/>"; //Use memoryfor($i = 0;$i < 100000;$i++){ $array[] = md5($i); } echo "Memory Status:".format_bytes(memory_get_usage())."<br/>"; echo "Delete half of the memory<br/>"; //Delete half of the memoryfor($i = 0;$i < 100000;$i++){ unset($array[$i]); } echo "Final Memory State:".format_bytes(memory_get_usage()); echo "<hr/>"; echo "Memory Peak Status:".format_bytes(memory_get_peak_usage());
Execution results:
Memory initial state: 65.27 KB
--------------------------------------------------------------------------------
Start using memory
Memory status: 12.01 MB
Delete half of the memory
Final memory state: 577.52 KB
--------------------------------------------------------------------------------
Memory peak state: 12.01 MB
For more information about PHP related content, please check out the topic of this site:Summary of common functions and techniques for php》、《Summary of usage of php strings》、《Complete collection of PHP array (Array) operation techniques》、《PHP data structure and algorithm tutorial"and"Summary of PHP Programming Algorithm》
I hope this article will be helpful to everyone's PHP programming.