Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

memory_get_usage

Returns the amount of memory the program uses.

Syntax

memory_get_usage(bool $real_usage = false): int

Parameters

ParameterTypeDescription
$real_usageboolOptional. `true` returns the memory allocated by the system.

Return value

intThe number of bytes.

Examples

How much memory an array takes:

<?php
 
$before = memory_get_usage();
 
$numbers = range(1, 100000);
 
echo round((memory_get_usage() - $before) / 1024 / 1024, 1), ' MB';

Notes

memory_get_peak_usage() returns the highest value during the run. The memory_limit setting defines the cap.

See also

  • hrtime — Returns a high-resolution timer reading in nanoseconds.
  • ini_set — Changes a PHP setting for the duration of the program.
  • range — Creates an array holding a sequence of values.

Related recipes

In the tutorial

  • Generators — Generators in PHP — the yield keyword, saving memory, keys, infinite sequences and reading large files.

Thanks for reading!

Donate to the site — we work hard for you.