PHP 内存监控


PHP 内存监控


正文

php获取内存使用量主要用 memory_get_usage()memory_get_peak_usage() 这两个函数。

memory_get_usage() 原型:

/**
 * Returns the amount of memory allocated to PHP
 * @link https://php.net/manual/en/function.memory-get-usage.php
 * @param bool $real_usage [optional] <p>
 * Set this to true to get the real size of memory allocated from
 * system. If not set or false only the memory used by
 * emalloc() is reported.
 * </p>
 * @return int the memory amount in bytes.
 */
function memory_get_usage ($real_usage = null) {}

memory_get_usage() 返回当前分配给PHP脚本的内存总量,单位是字节(byte)。 有一个参数$real_usage,将其设置为true,以获取从中系统分配的内存的实际大小;如果未设置或设置为false,则仅使用C函数 emalloc() 记录的值。

memory_get_peak_usage() 原型:

/**
 * Returns the peak of memory allocated by PHP
 * @link https://php.net/manual/en/function.memory-get-peak-usage.php
 * @param bool $real_usage [optional] <p>
 * Set this to true to get the real size of memory allocated from
 * system. If not set or false only the memory used by
 * emalloc() is reported.
 * </p>
 * @return int the memory peak in bytes.
 */
function memory_get_peak_usage ($real_usage = null) {}

memory_get_peak_usage() 返回内存使用峰值,单位是字节(byte)。 有一个参数$real_usage,将其设置为true,以获取从中系统分配的内存的实际大小;如果未设置或设置为false,则仅使用C函数 emalloc() 记录的值。

使用示例

$byte = memory_get_peak_usage(true);
$peak = number_format(bcdiv($byte, 1024, 4), 2) . "KB";
$byte = memory_get_usage(true);
$current = number_format(bcdiv($byte, 1024, 4), 2) . "KB";
$byte = memory_get_usage();
$php = number_format(bcdiv($byte, 1024, 4), 2) . "KB";

$mem = ['memory-peak' => $peak, 'memory-current' => $current, 'php-current' => $php];

bcdiv 是 bcmath拓展 的一个函数,用于求2个任意精度的数字除法计算: bcdiv ( string $left_operand , string $right_operand [, int $scale = int ] ) : string 左操作数除以右操作数, scale 参数用于设置结果中小数点后的小数位数,如果未设置,则默认为 0。

bcdiv()原型:

/**
 * Divide two arbitrary precision numbers
 * @link https://php.net/manual/en/function.bcdiv.php
 * @param string $dividend <p>
 * The dividend, as a string.
 * </p>
 * @param string $divisor <p>
 * The divisor, as a string.
 * </p>
 * @param int $scale [optional] <p>
 * This optional parameter is used to set the number of digits after the
 * decimal place in the result. If omitted, it will default to the scale
 * set globally with the {@link bcscale()} function, or fallback to 0 if
 * this has not been set.
 * </p>
 * @return string|null the result of the division as a string, or <b>NULL</b> if
 * <i>divisor</i> is 0.
 */
function bcdiv ($dividend, $divisor, $scale = 0) {}

number_format() ,格式化一个数字:

number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," ) : string

$number 你要格式化的数字, $decimals 要保留的小数位数, $dec_point 指定小数点显示的字符, $thousands_sep 指定千位分隔符显示的字符。

number_format(1234.5601, 2, ',', ' '); 为 1 234,56。

number_format()原型:

/**
 * Format a number with grouped thousands
 * @link https://php.net/manual/en/function.number-format.php
 * @param float $number <p>
 * The number being formatted.
 * </p>
 * @param int $decimals [optional] <p>
 * Sets the number of decimal points.
 * </p>
 * @param string $dec_point [optional]
 * @param string $thousands_sep [optional]
 * @return string A formatted version of number.
 */
function number_format ($number , $decimals = 0 , $dec_point = '.' , $thousands_sep = ',' ) {}






参考资料

memory_get_usage https://php.net/manual/en/function.memory-get-usage.php

memory_get_peak_usage https://www.php.net/manual/en/function.memory-get-peak-usage.php

获取PHP的内存使用情况 https://www.php.cn/php-weizijiaocheng-383493.html

bcdiv https://www.php.net/manual/zh/function.bcdiv.php

number_format https://php.net/manual/en/function.number-format.php


返回