readSystemMemoryBytes reads total physical memory from /proc/meminfo on Linux. Returns 0 if the file cannot be read or parsed (e.g., on non-Linux systems).
()
| 30 | // readSystemMemoryBytes reads total physical memory from /proc/meminfo on Linux. |
| 31 | // Returns 0 if the file cannot be read or parsed (e.g., on non-Linux systems). |
| 32 | func readSystemMemoryBytes() uint64 { |
| 33 | f, err := os.Open("/proc/meminfo") |
| 34 | if err != nil { |
| 35 | return 0 |
| 36 | } |
| 37 | defer func() { _ = f.Close() }() |
| 38 | |
| 39 | scanner := bufio.NewScanner(f) |
| 40 | for scanner.Scan() { |
| 41 | line := scanner.Text() |
| 42 | if strings.HasPrefix(line, "MemTotal:") { |
| 43 | var kb uint64 |
| 44 | if _, err := fmt.Sscanf(line, "MemTotal: %d kB", &kb); err == nil { |
| 45 | return kb * 1024 |
| 46 | } |
| 47 | return 0 |
| 48 | } |
| 49 | } |
| 50 | return 0 |
| 51 | } |
| 52 | |
| 53 | var ( |
| 54 | autoMemoryLimitOnce sync.Once |