Content · Web Development · PHP

PHP memory_reset_peak_usage() Function: Usage and Examples

Resets peak memory measurement so later memory_get_peak_usage() tracks a specific phase.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Admin

Resets peak memory measurement so later memory_get_peak_usage() tracks a specific phase.

What It Does and When to Use It


Measure a phase with a fresh peak baseline. Current allocations answer how much PHP holds now; peak allocations expose workload spikes that can exceed the process limit.

Requirements, Inputs and Expected Behavior


Confirm the runtime, extension, environment variables, and permissions before running the examples in production. Available since PHP 8.2; reset does not decrease actual current memory use.

Example 1: Basic Operation


Measure a phase with a fresh peak baseline.

<?php
memory_reset_peak_usage();
 $payload = range(1, 5_000);
 echo memory_get_peak_usage(true);
 unset($payload);

Example 2: Behavior and Validation


Track a second phase without the first phase peak.

<?php
$first = str_repeat('x', 1_000_000);
 unset($first);
 memory_reset_peak_usage();
 $second = str_repeat('y', 400_000);
 echo memory_get_peak_usage(false);

Practical Example: Application Use


Wrap a deterministic task in a per-phase memory measurement.

<?php
function measured(callable $task): int {
     memory_reset_peak_usage();
     $task();
     return memory_get_peak_usage(true);
 }
 echo measured(static function (): void { $items = range(1, 2000); });

Important Behavior and Edge Cases


The second example highlights: Track a second phase without the first phase peak. The practical example adds: Wrap a deterministic task in a per-phase memory measurement.

Comparison with Related Approaches


ApproachWhat changes
memory_get_usage versus memory_get_peak_usageCurrent allocations answer how much PHP holds now; peak allocations expose workload spikes that can exceed the process limit.
Choosing safelyMeasure the representative workload before changing memory_limit or forcing garbage collection.

Common Beginner Mistakes


MistakeCorrect approach
Assuming successful execution proves the result is usableCheck the return value, required extension, and observable output of the specific operation.
Ignoring runtime and environment differencesVerify PHP version, SAPI, operating system, service access, and permissions before deployment.
Skipping operational constraintsAvailable since PHP 8.2; reset does not decrease actual current memory use.

Key Takeaways


  • Resets peak memory measurement so later memory_get_peak_usage() tracks a specific phase.
  • Wrap a deterministic task in a per-phase memory measurement.
  • Measure the representative workload before changing memory_limit or forcing garbage collection.
Share this resourceSend this page to someone who may find it useful.