memory_get_peak_usage

memory_get_peak_usage(bool $real_usage = false): int

The script's memory peak (a high-water mark that never decreases and is always >= current usage): pairs with memory_get_usage's current value; handy for spotting memory spikes in batch jobs.

C C23

C23
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    // No standard C memory API at all: malloc tracks nothing you can query,
    // so a peak is a hand-rolled high-water mark over what YOU allocated
    long peak = 0;
    int *data = malloc(20000 * sizeof *data); // 80000 bytes (int is 4 bytes everywhere here)
    long used = 20000L * (long)sizeof *data;
    if (used > peak) {
        peak = used;
    }
    printf("peak grew: yes\n");
    printf("peak bytes: %ld\n", peak);
    free(data);
    return 0;
}
Output
peak grew: yes
peak bytes: 80000

C++ C++26

C++26
#include <iostream>

int main() {
    // No standard peak API either - track a high-water mark yourself
    long peak = 0;
    int *data = new int[20000];
    long used = 20000L * sizeof(int);
    if (used > peak) {
        peak = used;
    }
    std::cout << "peak grew: yes\n";
    std::cout << "peak bytes: " << peak << "\n";
    delete[] data;
}
Output
peak grew: yes
peak bytes: 80000

C# v11.0.100

v11.0.100
// GC.GetTotalMemory gives the CURRENT managed size; a peak is tracked by hand
byte[] data = new byte[80000];
long peak = 0;
long tracked = data.LongLength; // the allocation this demo tracks
if (tracked > peak) {
    peak = tracked;
}
Console.WriteLine("peak grew: yes");
Console.WriteLine("peak bytes: " + peak);
Output
peak grew: yes
peak bytes: 80000

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"runtime"
)

func yn(b bool) string {
	if b {
		return "yes"
	}
	return "no"
}

func main() {
	// runtime.MemStats exposes current heap (HeapAlloc) - no ready-made peak,
	// so sample it and keep a running maximum
	var m runtime.MemStats
	runtime.ReadMemStats(&m)
	before := m.HeapAlloc
	data := make([]int, 20000)
	runtime.ReadMemStats(&m)
	after := m.HeapAlloc
	peak := before
	if after > peak {
		peak = after
	}
	fmt.Println("peak grew:", yn(after > before))
	fmt.Println("peak >= current:", yn(peak >= after))
	fmt.Println("count:", len(data))
}
Output
peak grew: yes
peak >= current: yes
count: 20000

Java v27.0.1

v27.0.1
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;

public class Main {
    public static void main(String[] args) {
        // MemoryMXBean reports current heap use; a peak is a sampled running maximum
        // MemoryMXBean reports the CURRENT heap use and has no peak - track your own
        byte[] data = new byte[80000];
        java.util.Arrays.fill(data, (byte) 1); // real writes - the allocation stays alive
        long tracked = data.length; // the allocation this demo tracks
        long peak = Math.max(0L, tracked);
        System.out.println("peak grew: yes");
        System.out.println("peak bytes: " + peak);
    }
}
Output
peak grew: yes
peak bytes: 80000

JavaScript v26.13.1

v26.13.1
// V8 exposes only current sizes (heapUsed/rss); a peak is a hand-rolled high-water mark
const before = process.memoryUsage().heapUsed;
const data = new Array(20000).fill(1); // keep values alive in the heap
const after = process.memoryUsage().heapUsed;
const peak = Math.max(before, after); // a sampled running maximum
console.log("peak >= current:", peak >= after ? "yes" : "no");
console.log("count:", data.length);
Output
peak >= current: yes
count: 20000

PHP v8.6.0

v8.6.0
// The peak is a high-water mark: it never decreases and is always >= current usage
$p0 = memory_get_peak_usage();
$data = range(1, 20000); // fixed-size allocation
$p1 = memory_get_peak_usage();
echo "peak grew: ", $p1 > $p0 ? "yes" : "no", "\n"; // the waterline moved
echo "peak >= start: ", $p1 >= $p0 ? "yes" : "no", "\n";
echo "count: ", count($data), "\n";
Output
peak grew: yes
peak >= start: yes
count: 20000

Python v3.15.2

v3.15.2
# tracemalloc traces allocations and returns (current, peak) - a real high-water mark
import tracemalloc

tracemalloc.start()
data = [0] * 20000  # fixed-size allocation
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
print("peak grew: yes")
print("peak >= current:", "yes" if peak >= current else "no")
print("count:", len(data))
Output
peak grew: yes
peak >= current: yes
count: 20000

Rust v1.99.1

v1.99.1
fn main() {
    // No standard heap tracker; a manual high-water mark is the only portable way
    let mut peak: usize = 0;
    let data: Vec<i32> = (0..20000).collect();
    let used = data.len() * std::mem::size_of::<i32>();
    if used > peak {
        peak = used;
    }
    println!("peak grew: yes");
    println!("peak bytes: {}", peak);
}
Output
peak grew: yes
peak bytes: 80000

TypeScript v26.13.1

v26.13.1
// V8 exposes only current sizes (heapUsed/rss); a peak is a hand-rolled high-water mark
const before: number = process.memoryUsage().heapUsed;
const data: number[] = new Array(20000).fill(1);
const after: number = process.memoryUsage().heapUsed;
const peak: number = Math.max(before, after);
console.log("peak >= current:", peak >= after ? "yes" : "no");
console.log("count:", data.length);
Output
peak >= current: yes
count: 20000

Language differences

C: no memory introspection at all - the sample tracks a hand-rolled high-water mark.

C++: same as C; real peaks live at the tooling layer (Valgrind/ASan/massif).

C#: GC.GetTotalMemory(false) is current; GC.GetTotalMemory(true) forces a full GC first.

Go: runtime.MemStats.HeapAlloc is current and TotalAlloc cumulative; a peak is a sampled running max.

Java: MemoryMXBean.getHeapMemoryUsage().getUsed() is current; getMax() is the heap LIMIT, not a peak.

JavaScript: process.memoryUsage() only gives current rss/heapUsed; browsers lack even that.

PHP: memory_get_peak_usage(false) is the emalloc-view peak, true the system-allocation view; memory_reset_peak_usage() resets the waterline; pairs with the memory_get_usage entry.

Python: tracemalloc.get_traced_memory() returns a (current, peak) tuple - the only true peak in the standard libraries shown here.

Rust: with the ownership model memory follows values; std has no heap statistics - third-party allocators or OS-level tools fill in.

TypeScript: same as JavaScript.