396 of 410 menu

The memory_get_peak_usage Function

The memory_get_peak_usage function returns the peak memory usage by the script in bytes. Can be used for debugging memory consumption and finding bottlenecks in code. Takes one optional parameter that determines whether to return the real allocated memory (true) or the PHP emulated one (false).

Syntax

memory_get_peak_usage([bool $real_usage = false]);

Example

Get the peak memory usage in the script:

<?php // Create an array consuming memory $arr = range(1, 100000); // Get peak memory usage $peak = memory_get_peak_usage(); echo 'Peak memory usage: ' . $peak . ' bytes'; ?>

Code execution result:

'Peak memory usage: 14680064 bytes'

Example

Get the real peak memory usage (without considering PHP optimizations):

<?php $arr = range(1, 100000); $peak = memory_get_peak_usage(true); echo 'Real peak memory usage: ' . $peak . ' bytes'; ?>

Code execution result:

'Real peak memory usage: 20971520 bytes'

See Also

byenru