Function totalSystemMemory [src]

Returns the total system memory, in bytes as a u64. We return a u64 instead of usize due to PAE on ARM and Linux's /proc/meminfo reporting more memory when using QEMU user mode emulation.

Prototype

pub fn totalSystemMemory() TotalSystemMemoryError!u64

Possible Errors

UnknownTotalSystemMemory

Source

pub fn totalSystemMemory() TotalSystemMemoryError!u64 { switch (native_os) { .linux => { return totalSystemMemoryLinux() catch return error.UnknownTotalSystemMemory; }, .freebsd => { var physmem: c_ulong = undefined; var len: usize = @sizeOf(c_ulong); posix.sysctlbynameZ("hw.physmem", &physmem, &len, null, 0) catch |err| switch (err) { error.NameTooLong, error.UnknownName => unreachable, else => return error.UnknownTotalSystemMemory, }; return @as(usize, @intCast(physmem)); }, .openbsd => { const mib: [2]c_int = [_]c_int{ posix.CTL.HW, posix.HW.PHYSMEM64, }; var physmem: i64 = undefined; var len: usize = @sizeOf(@TypeOf(physmem)); posix.sysctl(&mib, &physmem, &len, null, 0) catch |err| switch (err) { error.NameTooLong => unreachable, // constant, known good value error.PermissionDenied => unreachable, // only when setting values, error.SystemResources => unreachable, // memory already on the stack error.UnknownName => unreachable, // constant, known good value else => return error.UnknownTotalSystemMemory, }; assert(physmem >= 0); return @as(u64, @bitCast(physmem)); }, .windows => { var sbi: windows.SYSTEM_BASIC_INFORMATION = undefined; const rc = windows.ntdll.NtQuerySystemInformation( .SystemBasicInformation, &sbi, @sizeOf(windows.SYSTEM_BASIC_INFORMATION), null, ); if (rc != .SUCCESS) { return error.UnknownTotalSystemMemory; } return @as(u64, sbi.NumberOfPhysicalPages) * sbi.PageSize; }, else => return error.UnknownTotalSystemMemory, } }