Measure Current and Peak PHP Memory Usage at Script Checkpoints
Track PHP memory in bytes at named checkpoints, compare current and peak readings, and inspect real_usage alongside the defaults.
To see how a PHP script’s memory readings change, call memory_get_usage() and memory_get_peak_usage() at named points in the code. The first gives a current reading; the second gives the highest reading reached so far. Print both at each checkpoint. Otherwise, a peak left behind by an earlier step can look like memory used by the step you just completed.
Record current and peak memory at named checkpoints
The PHP manual documents both functions as returning integers, and documents the peak value in bytes. Keep the byte values in your checkpoint output. A MiB conversion can make them easier to scan, but it should not replace the original readings when you want to compare steps.
This runnable example takes a reading before building an array, after building it, and after unset():
<?php
function checkpoint(string $label): void
{
$currentBytes = memory_get_usage();
$peakBytes = memory_get_peak_usage();
echo $label . " | current: " . $currentBytes . " bytes"
. " (" . round($currentBytes / 1024 / 1024, 2) . " MiB)"
. " | peak: " . $peakBytes . " bytes"
. " (" . round($peakBytes / 1024 / 1024, 2) . " MiB)\n";
}
checkpoint('before array');
$values = [];
for ($i = 0; $i < 100000; $i++) {
$values[] = $i;
}
checkpoint('after array');
unset($values);
checkpoint('after unset');
Each printed line names the checkpoint and includes both readings in bytes. The conversion divides by 1024 twice, so its unit is MiB. The example does not prescribe output numbers: compare the readings your script prints rather than treating any particular byte count as an expected result.
Compare the array checkpoints without resetting the peak
Read the three lines as a sequence. Compare current at before array with current at after array to see the change across the array-building step. Then compare current at after array with current at after unset to see what the later checkpoint reports. These are readings of the script at those points, not a separately measured size for $values alone.
Now make the same comparison using peak, but ask a different question: what is the highest usage recorded by this point? The after unset peak cannot be lower than the earlier peak, even if the current reading falls. That is useful when a temporary high point matters, but it also means the last peak is not the memory used only by unset() or by the most recent line. A PHP memory example likewise reports a lower current reading after an array is removed while retaining a higher peak reading.
Interpret bytes and the optional real_usage flag
With no argument, each function uses real_usage = false. For the peak function, the PHP manual explains that the default reports memory used by emalloc(), while true reports the real size of memory allocated from the system. Use matching arguments when comparing a current reading with a peak reading. To inspect both views at one named checkpoint, add these lines to the script after the function definition:
echo "inspection | current default: " . memory_get_usage() . " bytes\n";
echo "inspection | peak default: " . memory_get_peak_usage() . " bytes\n";
echo "inspection | current real_usage: " . memory_get_usage(true) . " bytes\n";
echo "inspection | peak real_usage: " . memory_get_peak_usage(true) . " bytes\n";
The default and true readings answer related but different questions. Do not subtract one from a checkpoint recorded with the other and label the result as the array’s growth. Neither view is a reading of all memory in the machine: these functions concern memory allocated to PHP or its script, not total system memory.
Add checkpoints to a longer-running loop
If a loop has many iterations, record at a fixed interval rather than printing a line for every item. For example, the following fragment can go later in the first script, where checkpoint() has already been defined:
$values = [];
for ($i = 1; $i <= 50000; $i++) {
$values[] = $i;
if ($i % 10000 === 0) {
checkpoint("iteration $i");
}
}
The labels identify which completed iteration produced each pair of readings. Compare successive current values to see the reported change between intervals; scan peak for the highest point reached by each checkpoint. This interval view will not tell you which individual iteration produced a change between two printed lines. If that distinction matters, place another named checkpoint around the specific step you want to inspect.
