memory_get_usage
memory_get_usage(bool $real_usage = false): int
Returns the amount of memory currently allocated to the script (actual use by default; true includes unused pages held by the allocator).
C C23
#include <stdio.h>
#include <stdlib.h>
int main(void) {
/* No standard whole-heap introspection; malloc's return is the fact,
sizeof gives the type-level view. glibc's mallinfo2(3) is the
allocator-stats analogue (glibc-only, not portable) */
int *data = malloc(10000 * sizeof(int));
printf("%s\n", data != NULL ? "allocated" : "failed");
printf("%zu\n", sizeof(int));
free(data);
return 0;
}
allocated 4
C++ C++26
#include <iostream>
int main() {
// No standard heap introspection; operator new returns memory or throws
// (bad_alloc). Type-level sizing (sizeof) is the closest portable fact.
int* data = new int[10000];
std::cout << (data != nullptr ? "allocated" : "failed") << "\n";
std::cout << sizeof(int) << "\n";
delete[] data;
return 0;
}
allocated 4
C# v11.0.100
// GC.GetTotalMemory is the CLR managed-heap view; the GC moves freely,
// so only stable facts are asserted (delta before/after is non-assertable)
long before = GC.GetTotalMemory(false);
int[] data = new int[10000];
long after = GC.GetTotalMemory(false);
Console.WriteLine(data.Length == 10000);
Console.WriteLine(GC.GetTotalMemory(false) > 0);
True True
Go v1.27.4
package main
import (
"fmt"
"runtime"
)
func main() {
var before, after runtime.MemStats
runtime.ReadMemStats(&before)
slice := make([]int, 10000) // fixed-size allocation
runtime.ReadMemStats(&after)
fmt.Println(len(slice) == 10000)
// TotalAlloc is a cumulative counter - monotonic, safe to assert
fmt.Println(after.TotalAlloc > before.TotalAlloc)
}
true true
Java v27.0.1
// Runtime.totalMemory()-freeMemory() is the JVM used-heap view; the GC
// moves freely, so only stable facts are asserted
class Main {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
long before = rt.totalMemory() - rt.freeMemory();
int[] data = new int[10000]; // fixed-size allocation
long after = rt.totalMemory() - rt.freeMemory();
System.out.println(data.length == 10000);
System.out.println(rt.totalMemory() >= rt.freeMemory());
}
}
true true
JavaScript v26.13.1
// Node: process.memoryUsage().heapUsed is the V8 heap analogue of the
// used-memory view; RSS is the OS-level view. heapUsed is not monotonic
// (GC runs whenever it wants) - deltas are non-assertable
const before = process.memoryUsage().heapUsed;
const data = Array.from({ length: 10000 }, (_, i) => i);
const after = process.memoryUsage().heapUsed;
console.log(data.length === 10000);
console.log(before > 0 && after > 0);
true true
PHP v8.6.0
// Default: memory actually in use (emalloc'd); true: total handed out by
// the allocator including unused pages
$before = memory_get_usage();
$data = range(1, 10000); // fixed-size allocation
$after = memory_get_usage();
// Byte deltas can differ across platforms/builds - assert the boolean only
echo "delta positive: ", $after > $before ? "yes" : "no", "\n";
echo "array alive: ", count($data) === 10000 ? "yes" : "no", "\n";
echo "real >= internal: ", memory_get_usage(true) >= memory_get_usage() ? "yes" : "no";
delta positive: yes array alive: yes real >= internal: yes
Python v3.15.2
import sys
import tracemalloc
# tracemalloc is the whole-process analogue (PHP memory_get_usage);
# sys.getsizeof is the single-object view - two different scopes
tracemalloc.start()
before, _ = tracemalloc.get_traced_memory()
data = list(range(10000))
after, _ = tracemalloc.get_traced_memory()
print(after > before)
print(len(data) == 10000)
print(sys.getsizeof(data) > 0)
tracemalloc.stop()
True True True
Rust v1.99.1
// Rust has no standard memory introspection: ownership makes allocations
// a compile-time story (no GC, no allocator-visible global counter).
// The closest std fact: size_of for type-level sizing.
use std::mem::size_of;
fn main() {
let data: Vec<i32> = (1..=10000).collect(); // heap allocation via Vec
println!("{}", data.len() == 10000);
println!("{}", size_of::<Vec<i32>>() == 3 * size_of::<usize>()); // ptr+len+cap
}
true true
TypeScript v26.13.1
// Same runtime: heapUsed is best-effort; GC timing makes deltas
// non-assertable - only stable facts go to output
interface MemUsage {
heapUsed: number;
}
const mem: MemUsage = process.memoryUsage();
const data: number[] = Array.from({ length: 10000 }, (_, i) => i);
console.log(data.length === 10000);
console.log(mem.heapUsed > 0);
true true
Language differences
C: no standard memory introspection — malloc's return value is the only portable fact; glibc's mallinfo2(3) is the allocator-stats counterpart (glibc only); sizeof gives the type-level view (example).
C++: same as C (no standard heap introspection); operator new throws bad_alloc instead of returning NULL; third-party tooling exists (Valgrind/ASan).
C#: GC.GetTotalMemory(false) is the managed-heap view (the boolean argument parallels PHP's: true forces a full GC first); the GC moves freely, so deltas are non-assertable (example outputs stable facts only).
Go: runtime.ReadMemStats exposes the full heap picture — HeapAlloc is "currently in use" (PHP's default view), TotalAlloc is a cumulative counter (monotonic, safe to assert, as written); Sys approximates PHP's real view.
Java: Runtime.totalMemory()-freeMemory() is the JVM used heap; -Xmx caps it and Runtime.maxMemory() reports the cap.
JavaScript: Node's process.memoryUsage() is layered — heapUsed (V8 in use) / heapTotal (V8 allocated) / rss (OS-resident, near PHP's real view) / external (Buffer and other off-heap); --max-old-space-size caps it.
PHP: the two views are the core — the default is "emalloc'd actual use" (bytes the PHP allocator has in use), true is "total taken from the OS" (including unused pages, aligned with system-monitor views); measured locally, range(1,10000) grows actual use by roughly 260k bytes (it floats across platforms/builds, so the example outputs the boolean only); the family also has memory_get_peak_usage (historic peak) and memory_reset_peak_usage (reset, 8.2+).
Python: sys.getsizeof is the per-object view (includes the object header, ignores reference cycles); tracemalloc is whole-process tracing (as written, can localize allocation sites); resource.getrusage / psutil.Process().memory_info() are the OS layer.
Rust: no standard memory introspection — under the ownership model "how much memory is used" is a compile-time layout plus the allocator's (jemalloc etc.) private ledger; the language has no global counter; size_of/size_of_val are the only standard-level facts (example).
TypeScript: runtime identical to JavaScript (layered heapUsed/rss); types are shape annotations only.
— General conclusion: the meaning of "memory usage" shifts with the memory model — managed runtimes (PHP/Java/Go/C#) keep a runtime ledger you can query; manual management (C/C++/Rust) leaves only the allocator or syscalls; Python sits in between (object view + tracer).