php8.5
Home/ Manual/ info / functions/ memory_get_usage

memory_get_usage

PHP function Edit on GitHub ✎

(PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8)

Returns the amount of memory allocated to the PHP script, or reserved from the system by PHP's memory manager

Description

memory_get_usage(bool $real_usage = false): int

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.

Parameters

real_usage

Set 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.

If not set or false only the memory currently allocated to the PHP script is reported.

Note

PHP only tracks memory allocated through its own memory manager, that is through the internal emalloc() function and the related ones. Memory allocated persistently, or directly with malloc() by an extension, is not reported, even when real_usage is true.

Return Values

Returns the memory amount in bytes.

Examples

A memory_get_usage() example

php
<?php

// 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

Source: reference/info/functions/memory-get-usage.xml · from the official PHP manual (php/doc-en)