std/os/memory
std/os/src/memory.trb
Memory - how much memory the machine has and how much of it a new allocation could still get - and Swap, the
space on disk the system pages memory out to.
Memory.current is a match OperatingSystem.current whose arm hands the question to the directory of that system;
only the arm of the build's target is compiled.
type Memory
type Memory with Show, Equals
How much memory the machine has, and how much of it a new allocation could still get without swapping.
Examples
const memory = Memory.current()?
print "{memory.available} of {memory.total} available"
if memory.available.fraction(of: memory.total) < 0.1 {
print "low on memory"
}
Pitfalls
- Linux and macOS use free memory as a cache on purpose, so the memory nothing has claimed yet is small on a busy
machine and says little.
availableis the number to watch, andMemory.usedistotal - availablefor that reason, never a number of the system's own.
Related
Swap- the swap of the same answer.System.pageSize- the size of one page of this memory.
field total
total: ByteSize
The physical memory the system manages, which is a little less than what is installed.
field available
available: ByteSize
What a new allocation could still get without swapping: each system's own estimate - MemAvailable on Linux,
ullAvailPhys on Windows, the free, inactive, speculative and purgeable pages on macOS - which counts a cache the
system would give up, so it is more than the memory nobody uses.
field swap
swap: Swap
The swap, which is zero where the system has none.
fn used
fn used(): ByteSize
What is not available: total - available. No system's own number of used memory is read, because each counts
its caches differently, and this one agrees with Memory.available on every system.
fn current
static fn current(): Result<Memory, OsError>
The memory of this machine, now. Every system keeps these numbers in the kernel, so it never waits.
Errors
OsError.Malformedwhere/proc/meminfois not in the form Linux writes it.OsError.Unsupportedon FreeBSD, whose slice has not come yet, and in a browser, which cannot tell.OsError.Failedwhere the system's call fails, with its words.
Examples
const memory = Memory.current()?
print "{memory.used()} used, {memory.swap.used} swapped"
type Swap
type Swap with Show, Equals
The swap of the machine: the page files on Windows, the swap partitions and files on Linux, the swap files macOS makes as it needs them. It is never the commit limit of Windows, which is memory and page files together.
Examples
const memory = Memory.current()?
print "{memory.swap.used} of {memory.swap.total} swap used, {memory.swap.free()} free"
Related
Memory- what it belongs to.