native macOS codings agent orchestrator
1import Darwin.Mach
2import Foundation
3
4/// Reads the current process's physical memory footprint in megabytes.
5///
6/// Uses `phys_footprint` from `task_info(TASK_VM_INFO)` — the same value
7/// Activity Monitor reports for "Memory" and Apple's recommended metric for
8/// "real RAM pressure this app contributes" (it folds in compressed memory).
9nonisolated enum MemoryProbe {
10 static func physFootprintMegabytes() -> Int {
11 var info = task_vm_info_data_t()
12 var count = mach_msg_type_number_t(
13 MemoryLayout<task_vm_info_data_t>.size / MemoryLayout<integer_t>.size
14 )
15 let result = withUnsafeMutablePointer(to: &info) { pointer -> kern_return_t in
16 pointer.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { reboundPointer in
17 task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), reboundPointer, &count)
18 }
19 }
20 guard result == KERN_SUCCESS else { return 0 }
21 return Int(info.phys_footprint / (1024 * 1024))
22 }
23}