memory_get_usage()

memory_get_usage() — Returns the amount of memory allocated to the PHP script, or reserved from the system by PHP's memory manager.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Returns the amount of memory, in bytes, that is currently allocated to the PHP script by PHP's memory manager. The reported value is rounded up to the granularity of the allocator, so it is larger than the exact number of bytes that were requested.

Signature

memory_get_usage(bool $real_usage = false): int

Parameters

ParameterTypeWhat it takes
$real_usageboolSet this to true to get the total amount of memory that PHP's memory manager has reserved from the system, including pages that are not currently in use. This is the value that memory_limit is enforced against. It is not the amount of memory the operating system has given to the PHP process, which is typically much larger.

Return value

Returns the memory amount in bytes.

Example

A runnable snippet:

// This is only an example, the numbers below will // differ depending on the specific system echo memory_get_usage(), "\n"; // 36640 $a = str_repeat("Hello", 4242); echo memory_get_usage(), "\n"; // 57960 unset($a); echo memory_get_usage(), "\n"; // 36744

See also

memory_get_peak_usage()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page memory_get_usage() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.