Skip to content

Stdlib: memory_get_usage / memory_get_peak_usage (Zend allocator parity) #3134

Closed

Description

@PurHur

Summary

Implement memory_get_usage() and memory_get_peak_usage() so scripts and tests can introspect allocator usage like Zend PHP.

php-src reference

  • Zend/zend_alloc.c — zend_memory_usage(), zend_memory_peak_usage()
  • ext/standard/basic_functions.c — PHP_FUNCTION(memory_get_usage), PHP_FUNCTION(memory_get_peak_usage)

Current behavior (PurHur)

Not registered in ext/standard/Module.php.

Repro

<?php
$before = memory_get_usage();
$buf = str_repeat('x', 10000);
$after = memory_get_usage();
$peak = memory_get_peak_usage();
echo ($after > $before) ? "grew\n" : "flat\n";
echo ($peak >= $after) ? "peak_ok\n" : "peak_bad\n";

Zend PHP 8.x: grew and peak_ok.

This compiler: undefined function.

Acceptance

  • VM builtins returning int byte counts; real_usage parameter documented (Zend uses different semantics for emalloc vs malloc — match php-src when real_usage=true).
  • Monotonic peak tracking across request/runtime lifetime.
  • Compliance PHPT with ordering checks (after > before, peak >= after).
  • Register in Module.php; update capability matrix.

Notes

  • Initial implementation may use process-level approximations if full Zend heap accounting is not available; document any intentional subset in PHPT NOTES.
  • Do not use internal_* permanent stubs.

Activity

  1. added
    enhancementNew feature or request
    phase-4:stdlibPhase 4 – stdlib for web apps
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on May 29, 2026
  2. PurHur commented on May 31, 2026

    @PurHur
    OwnerAuthor

    claim: worker-lane-c — starting this run

  3. PurHur commented on May 31, 2026

    @PurHur
    OwnerAuthor

    Merged via PR #3915.

    Verification

    • Issue repro (Zend PHP + VM): grew / peak_ok after str_repeat('x', 10000)
    • ./vendor/bin/phpunit --filter memory_get_usage — OK (2 tests: VM compliance + AOT fixture)

    Notes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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