Description
On Apple Silicon Macs, the system.memory metricset reports system.memory.free, system.memory.actual.free and the derived used.pct / actual.used.pct values that do not match the host. Idle machines show system.memory.actual.used.pct around 0.8–0.9, which triggers memory alerts.
We think we found the cause in pkg/systemmetrics/metric/memory/memory_darwin.go, which converts page counts to bytes with a fixed shift of 12 bits (4096-byte pages):
kern := uint64(vmstat.inactive_count) << 12
free := uint64(vmstat.free_count) << 12
On Apple Silicon, host_statistics(HOST_VM_INFO) returns counts in kernel pages of 16384 bytes. sysctl hw.pagesize returns 16384, and vm_stat prints (page size of 16384 bytes). As a result, free and inactive are 4× too small, and used / actual.used are too large.
Is this behaviour expected, or are we missing something in our setup?
Example (24 GiB Mac mini, Apple Silicon, mostly idle)
vm_stat on the host:
Mach Virtual Memory Statistics: (page size of 16384 bytes)
Pages free: 119393.
Pages active: 524578.
Pages inactive: 521233.
Pages wired down: 164096.
Pages occupied by compressor: 204071.
| metricbeat (pages × 4096) | pages × 16384 | |
|---|---|---|
system.memory.free |
0.46 GiB | 1.82 GiB |
| inactive | 1.99 GiB | 7.95 GiB |
system.memory.used.pct |
0.981 | 0.924 |
system.memory.actual.used.pct |
0.898 | 0.593 |
top on the same host also reports far more unused memory than metricbeat's free. On our other Apple Silicon agents, the reported actual.used.pct is 0.79–0.87 while the hosts are idle; after correction it is 0.16–0.48. On Debian hosts, the values are consistent with free -m.
Possible fix
Use the actual page size instead of the hard-coded shift, for example vm_kernel_page_size, host_page_size() or sysctl hw.pagesize.
- gopsutil fixed the same bug in mem.VirtualMemory(): wrong page size on darwin arm64 by kyz · Pull Request #1334 · shirou/gopsutil · GitHub ("wrong page size on darwin arm64").
Versions
- Version: Metricbeat 8.19.x (darwin/arm64 build). The code on
mainis unchanged. - Operating System: macOS 27 on Apple Silicon (M4)
Steps to Reproduce
- Run Metricbeat with the
systemmodule and thememorymetricset on an Apple Silicon Mac. - Compare
system.memory.freewithvm_stat"Pages free" ×sysctl -n hw.pagesize. system.memory.freeis 4× lower, andsystem.memory.actual.used.pctis much higher than Activity Monitor's memory usage.
Configuration used:
metricbeat_version: 8.19.23
- module: system
period: 10s
metricsets:
- cpu
- load
- memory
cpu.metrics: ["percentages", "normalized_percentages"]