pub fn detect_total_ram_mb() -> Option<u64> {
#[cfg(target_os = "macos")]
{
detect_macos_ram_mb()
}
#[cfg(target_os = "linux")]
{
detect_linux_ram_mb()
}
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
{
None
}
}
#[cfg(target_os = "macos")]
fn detect_macos_ram_mb() -> Option<u64> {
use std::process::Command;
let output = Command::new("sysctl")
.args(["-n", "hw.memsize"])
.output()
.ok()?;
if !output.status.success() {
return None;
}
let text = String::from_utf8(output.stdout).ok()?;
let bytes: u64 = text.trim().parse().ok()?;
Some(bytes / (1024 * 1024))
}
#[cfg(target_os = "linux")]
fn detect_linux_ram_mb() -> Option<u64> {
let text = std::fs::read_to_string("/proc/meminfo").ok()?;
for line in text.lines() {
if let Some(rest) = line.strip_prefix("MemTotal:") {
let mut parts = rest.split_whitespace();
let kb: u64 = parts.next()?.parse().ok()?;
return Some(kb / 1024);
}
}
None
}