Current Location: Home> Function Categories> memory_get_usage

memory_get_usage

Returns the amount of memory allocated to PHP
Name:memory_get_usage
Category:PHP Options and Information
Programming Language:php
One-line Description:Gets the amount of memory consumed by the current PHP script, and the return value is in bytes

Function name: memory_get_usage()

Applicable versions: All versions

Usage: The memory_get_usage() function is used to obtain the amount of memory consumed by the current PHP script, and the return value is in bytes.

Example:

 // 示例1: 获取当前脚本消耗的内存量$memoryUsage = memory_get_usage(); echo "当前脚本消耗的内存量: " . $memoryUsage . " 字节"; // 示例2: 获取某个函数调用前后的内存变化function getMemoryUsageDiff() { $startMemory = memory_get_usage(); // 执行一些代码$endMemory = memory_get_usage(); $memoryDiff = $endMemory - $startMemory; echo "函数调用前后内存变化: " . $memoryDiff . " 字节"; } getMemoryUsageDiff();

Notes:

  • Since the amount of memory returned by the memory_get_usage() function is the total amount of memory consumed by the current script, you need to pay attention to when calculating the difference before and after the function call.
  • The unit of the return value is bytes and can be converted as needed.
  • This function is available in all versions of PHP without additional extensions or configuration.
Similar Functions
Popular Articles