Current Location: Home> Function Categories> memory_get_peak_usage

memory_get_peak_usage

Returns the peak memory allocated by PHP
Name:memory_get_peak_usage
Category:PHP Options and Information
Programming Language:php
One-line Description:Get the peak memory usage during the current script execution

Function name: memory_get_peak_usage()

Applicable version: PHP 5 >= 5.2.0, PHP 7

Usage: The memory_get_peak_usage() function is used to obtain the memory peak usage during the current script execution. It returns an integer representing the memory usage in bytes.

Example:

 // 示例1:获取当前脚本的内存峰值使用量$peakUsage = memory_get_peak_usage(); echo "当前脚本的内存峰值使用量为:" . $peakUsage . " 字节"; // 示例2:获取某个函数或代码段的内存峰值使用量function myFunction() { $data = str_repeat("Hello", 1000000); // 生成一个较大的字符串echo "当前函数的内存峰值使用量为:" . memory_get_peak_usage() . " 字节"; } myFunction();

Notes:

  • This function may differ in different operating systems and PHP configurations.
  • Since memory usage is calculated based on the execution process of the current script, the memory peak usage of the same piece of code may vary in different environments.
  • This function can be called multiple times at different locations in the code to get memory usage at different points in time for performance optimization or memory management.
Similar Functions
Popular Articles