memory
import std::process::memory; · source
The machine's physical memory: how much it has, and how much is still available to a program that starts now. What one child used at its peak is ExitStatus.peak_memory_bytes.
import std::process::memory::{ SystemMemory };
const memory: SystemMemory = SystemMemory::query()?;
const spare_megabytes: u64 = memory.available_bytes() / 1048576;
Types
SystemMemory
type struct SystemMemory {
private total: u64;
private available: u64;
private committed: u64;
private commit_limit: u64;
}
query() reads the figures once: MemTotal and MemAvailable from /proc/meminfo on Linux, GlobalMemoryStatusEx on Windows. On Linux it fails with the error reading the file gave, or InvalidData when either line is missing or not a number. total_bytes() is the physical memory installed; available_bytes() is what a program could still be given without the system paging anything out.
Constructors
static query() -> Result<SystemMemory, IoError>;
static query() -> Result<SystemMemory, IoError>;Read the machine's memory now: MemTotal, MemAvailable, Committed_AS and CommitLimit from /proc/meminfo on Linux, GlobalMemoryStatusEx and GetPerformanceInfo on Windows. Fails with the error reading /proc/meminfo gave, or InvalidData when any of those lines is missing or not a number.
static query() -> Result<SystemMemory, IoError>;
static query() -> Result<SystemMemory, IoError>;Read the machine's memory now: the total and available physical memory GlobalMemoryStatusEx reports, and the commit charge and its limit GetPerformanceInfo reports (/proc/meminfo on Linux). Fails with the system's error when either call fails.
Methods
total_bytes(&this) -> u64;
total_bytes(&this) -> u64;The physical memory installed, in bytes.
available_bytes(&this) -> u64;
available_bytes(&this) -> u64;The physical memory a program could still be given without the system paging anything out, in bytes.
committed_bytes(&this) -> u64;
committed_bytes(&this) -> u64;The memory the system has promised to every running process, in bytes: what they have allocated, whether or not they have touched it yet. Windows calls it the commit charge; it counts against physical memory and the page file together, and an allocation that would take it past commit_limit_bytes fails. On Linux it is Committed_AS, which may exceed the limit, since Linux promises more than it has unless told not to overcommit.
commit_limit_bytes(&this) -> u64;
commit_limit_bytes(&this) -> u64;The most memory the system will promise, in bytes: physical memory and the page file (Windows), or the limit Linux enforces when it is told not to overcommit (CommitLimit).
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
namespace std::process::memory;The machine's memory: how much physical memory it has and how much is still available to a program that starts now, and its commit - the memory the system has promised to every running process - beside the most it will promise.
module pageconst NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const memory: SystemMemorystatic SystemMemory::query() -> Result<SystemMemory, IoError>Read the machine's memory now: MemTotal, MemAvailable, Committed_AS and CommitLimit from /proc/meminfo on Linux, GlobalMemoryStatusEx and GetPerformanceInfo on Windows. Fails with the error reading /proc/meminfo gave, or InvalidData when any of those lines is missing or not a number.
value?Error propagation. On a Result<T, E> it yields the T of an Ok and otherwise returns the Err(e) from the enclosing function unchanged; on an Option<T> it yields the T of a Some or returns None. It works on any type implementing Try, in a function whose return type implements FromResidual of it; an error of another type converts through From.
const spare_megabytes: u64const memory: SystemMemorySystemMemory::available_bytes(&this) -> u64The physical memory a program could still be given without the system paging anything out, in bytes.
std::process::memorytype struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct SystemMemory {}The machine's memory, read once by query.
private type struct Name { ... }
private:Confined to the module that declares it - for items and members alike. A private field or function reached from another module is E0353, and a private type named there is E0503. private: opens a block of such members.
SystemMemory.total: u64SystemMemory.available: u64SystemMemory.committed: u64SystemMemory.commit_limit: u64IoErrorKind::InvalidDataThe bytes do not have the form the operation needs, such as text that is not UTF-8.
std::io::errorstatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
type enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.
type struct IoError {
kind: IoErrorKind;
os_code: i32;
}The error of a failed I/O operation: its kind, the operating system's own code, and, when the operation named one, what it was doing and the path. It prints as could not open config.toml: not found.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
SystemMemory::committed_bytes(&this) -> u64The memory the system has promised to every running process, in bytes: what they have allocated, whether or not they have touched it yet. Windows calls it the commit charge; it counts against physical memory and the page file together, and an allocation that would take it past commit_limit_bytes fails.
SystemMemory::commit_limit_bytes(&this) -> u64The most memory the system will promise, in bytes: physical memory and the page file (Windows), or the limit Linux enforces when it is told not to overcommit (CommitLimit).