Skip to content

Stdlib: memory_reset_peak_usage() — peak counter not reset after free (VmMemory RSS, pairs #5108) #7310

@PurHur

Description

@PurHur

Category

stdlib

Problem

memory_reset_peak_usage() is registered (#5539) and accepts zero arguments, but memory_get_peak_usage() does not drop after a large allocation is freed — unlike Zend. Issue #5108 still describes the function as missing; the remaining gap is behavioral parity of the peak counter reset.

Root cause: ext/standard/VmMemory.php tracks peak via RSS approximation (readRssBytes()). Freeing PHP variables does not shrink RSS, so resetPeakUsage() baselines peak at the still-high current RSS and get_peak_usage() reports reset_fail.

php-src reference

Repro (failure today)

<?php
$before = memory_get_peak_usage();
$str = str_repeat('x', 1 << 20);
$peak = memory_get_peak_usage();
unset($str);
memory_reset_peak_usage();
$after = memory_get_peak_usage();
echo ($peak > $before ? "peak_grew\n" : "peak_flat\n");
echo ($after < $peak ? "reset_ok\n" : "reset_fail\n");
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php bin/vm.php repro.php
php repro.php  # Zend in same container
'

Observed (VM, 2026-06-07): peak_grew + reset_fail.

Observed (Zend PHP 8.2 in container): peak_grew + reset_ok.

Also missing: bool $real_usage = false parameter and return value (tracked separately in #7247 MemoryUsage enum work).

Scope (PHP-in-PHP)

Path Work
ext/standard/VmMemory.php Track emalloc-style peak counters independently of RSS
ext/standard/JitMemory.php / lib/JIT/Builtin/MemoryRuntime.php Mirror reset semantics in JIT/AOT
Tests extend test/compliance/cases/stdlib/memory_reset_peak_usage.phpt

Implementation hints

php-src-strict

Default CI/compliance must match Zend peak reset after unset(); no php-compiler-strict shortcut unless static proof shows no user-visible peak introspection.

Done when

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter memory_reset_peak_usage'

Pairs

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions