Skip to content

Commit 07297e9

Browse files
committed
Replace iOS-only memory probe with host_statistics64
os_proc_available_memory is unavailable on macOS and broke App CI. Use free+inactive pages from HOST_VM_INFO64 instead; return 0 on probe failure.
1 parent 488889c commit 07297e9

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

Sources/VocaMac/Services/SystemInfo.swift

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,30 @@ enum SystemInfo {
149149
return max(2, min(cores / 2, 8))
150150
}
151151

152-
/// Bytes this process can still allocate. Zero means the value is unknown.
152+
/// Approximate reclaimable free memory in bytes. Zero means the probe failed.
153153
static var availableMemoryBytes: UInt64 {
154-
let available = os_proc_available_memory()
155-
guard available > 0 else { return 0 }
156-
return UInt64(available)
154+
var stats = vm_statistics64()
155+
var count = mach_msg_type_number_t(
156+
MemoryLayout<vm_statistics64_data_t>.stride / MemoryLayout<integer_t>.stride
157+
)
158+
let host = mach_host_self()
159+
let kr = withUnsafeMutablePointer(to: &stats) {
160+
$0.withMemoryRebound(to: integer_t.self, capacity: Int(count)) {
161+
host_statistics64(host, HOST_VM_INFO64, $0, &count)
162+
}
163+
}
164+
guard kr == KERN_SUCCESS else { return 0 }
165+
var pageSize: vm_size_t = 0
166+
guard host_page_size(host, &pageSize) == KERN_SUCCESS, pageSize > 0 else { return 0 }
167+
let pages = UInt64(stats.free_count) + UInt64(stats.inactive_count)
168+
return pages * UInt64(pageSize)
157169
}
158170

159171
/// Whether loading `size` is likely to fit without thrashing.
160172
///
161173
/// Uses the catalog RAM estimate against installed memory and against
162-
/// `os_proc_available_memory()`. A zero available reading is treated as
163-
/// unknown so we do not block loads on a failed probe.
174+
/// reclaimable free memory from host_statistics64. A zero available
175+
/// reading is treated as unknown so we do not block loads on a failed probe.
164176
static func canFitModelInMemory(
165177
_ size: ModelSize,
166178
physicalMemoryGB: Int = physicalMemoryGB,

0 commit comments

Comments
 (0)