ADR-400: mem::allocations() and mem::usage() Measure a Thread’s Memory, Not Its Addresses

Status: Accepted Date: November 18, 2026 Answers: how a test sees that an operation kept a buffer or copied it, which “Unobservable std tests” (#2088) found no way to say in Nikaia Related: ADR-c-boundary-v1 (unsafe and extern only for C calls, never a raw pointer); ADR-327 D9, D10 (the program’s counting allocator); ADR-371 and Part III Chapter 16 (architecture functions in std::backend::<arch>); views-v4 D22 (it needed no std::mem; this record adds the module for measuring); Part I, update (copy or address is the compiler’s choice)

1. Question

test "joining keeps the left side's buffer" {
    let mut left = String(capacity: 64)
    left.push("left-")
    let joined = left + "right"
    // did `+` reuse left's buffer, or allocate a new one?
}

std’s Rust tests check this by comparing as_ptr() before and after. A Nikaia program has no address of a value, and a test of the language’s own library is to be a Nikaia program (#821).

2. Options

Option Why it lost See
A value’s address as a number, readable without unsafe it leaks the memory layout (it defeats address randomisation), and it pins what the compiler may decide: whether a value is copied or shared is the compiler’s choice (Part I, update) D3
Rust inside an unsafe block unsafe is for C calls only (ADR-c-boundary-v1); a test written in Rust is not a Nikaia test D3
An address of a type that allows only == a type with no other use is foreign to the language (owner, November 18, 2026) D3
mem::keeps_memory(value, fn), the operation passed as a function the function-argument form reads badly in a test (owner) D1
mem::same_memory(a, b) the left side of left + "right" is moved; nothing is left to compare with D1
Bytes in use alone a copy that frees the old buffer leaves the bytes in use unchanged D1

3. Decision

D1 — mem::allocations() counts the calling thread’s allocations.

std::mem::allocations() -> int is how many times the calling thread has requested memory since it started. A test reads it before and after an operation; equal values mean the operation requested nothing:

let before = mem::allocations()
let joined = left + "right"
assert mem::allocations() == before

It counts per thread, so std’s own helper threads (the runtime’s workers, a timer) do not change it while a test runs.

D2 — mem::usage() gives the calling thread’s bytes in use.

std::mem::usage() -> int is the bytes the calling thread holds now: what it allocated less what it freed. Read before and after, it shows what an operation kept (as PHP’s memory_get_usage does). A free on another thread than the allocating one counts on the freeing thread (ADR-327 D9), so the value can be negative on a thread that frees more than it allocated.

D3 — No address is readable.

Neither function hands out an address. A value’s address as a number belongs with the hardware-near work of std::backend::<arch> (ADR-371), and writing through an address with unsafe; both are a later question, not decided here.

4. Reasons

5. Consequences

Decided November 18, 2026 (owner).

This site is open source. Improve this page.