Detect total system memory in bytes by reading `/proc/meminfo`. Returns `None` if the file cannot be read or parsed (non-Linux, containers with restricted procfs, etc.). Callers should fall back to a hardcoded default.
()
| 218 | /// Returns `None` if the file cannot be read or parsed (non-Linux, containers |
| 219 | /// with restricted procfs, etc.). Callers should fall back to a hardcoded default. |
| 220 | fn detect_system_memory_bytes() -> Option<u64> { |
| 221 | let contents = std::fs::read_to_string("/proc/meminfo").ok()?; |
| 222 | for line in contents.lines() { |
| 223 | if let Some(rest) = line.strip_prefix("MemTotal:") { |
| 224 | // Format: "MemTotal: 16384000 kB" |
| 225 | let kb_str = rest.trim().strip_suffix("kB")?.trim(); |
| 226 | let kb: u64 = kb_str.parse().ok()?; |
| 227 | return Some(kb * 1024); |
| 228 | } |
| 229 | } |
| 230 | None |
| 231 | } |
| 232 | |
| 233 | #[cfg(test)] |
| 234 | mod tests { |