The memory_get_usage Function

The memory_get_usage function returns the amount of memory (in bytes) that is currently allocated for the PHP script to work. It accepts one optional parameter - a flag that determines whether to include memory that has been allocated but not yet used in the result.

Syntax

phpmemory_get_usage([bool $real_usage = false]);

Example

Get the current memory usage:

php<?php
  echo memory_get_usage();
?>

Code execution result (example):

php2097152

Example

Compare memory usage before and after creating an array:

php<?php
  $mem1 = memory_get_usage();
  $arr = range(1, 10000);
  $mem2 = memory_get_usage();
  echo $mem2 - $mem1;
?>

Code execution result (example):

php528440

Example

Using the $real_usage parameter:

php<?php
  echo memory_get_usage(true);
?>

Code execution result (example):

php2097152

See Also

  • the memory_get_peak_usage function,
    which returns the peak memory usage value
  • the phpinfo function,
    which outputs PHP configuration information