func systemFreeMemorySize() -> UInt?
{
let HOST_VM_INFO_COUNT: mach_msg_type_number_t = mach_msg_type_number_t(MemoryLayout<vm_statistics_data_t>.size / MemoryLayout<integer_t>.size)
let host: host_t = mach_host_self()
var pageSize: vm_size_t = vm_size_t()
let hostPageSizeKernStatus: kern_return_t = host_page_size(host, &pageSize)
/*
guard hostPageSizeKernStatus == KERN_SUCCESS else {
NSLog("Error with host_page_size(): " + (String.fromCString(mach_error_string(hostPageSizeKernStatus)) ?? "unknown error"))
return nil
}*/
var stats: vm_statistics_data_t = vm_statistics_data_t()
var count: mach_msg_type_number_t = HOST_VM_INFO_COUNT
let kernStatus: kern_return_t = withUnsafeMutablePointer(to: &stats) {
// *** Error on following line:
return host_statistics(host, HOST_VM_INFO, host_info_t($0), &count)
}
/*
guard kernStatus == KERN_SUCCESS else {
NSLog("Error with host_statistics(): " + (String.fromCString(mach_error_string(kernStatus)) ?? "unknown error"))
return nil
} */
return UInt(stats.free_count) * UInt(pageSize)
}
In the line return host_statistics(host, HOST_VM_INFO, host_info_t($0), &count) there is an error: 'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.
How to fix this?
for Swift 3.1. (I added some more code to inspect all info you could need.)
func freeMemory()->UInt64 {
let host_port :mach_port_t = mach_host_self()
let HOST_BASIC_INFO_COUNT = MemoryLayout<host_basic_info>.stride/MemoryLayout<integer_t>.stride
var size = mach_msg_type_number_t(HOST_BASIC_INFO_COUNT)
var hostInfo = host_basic_info()
let status1 = withUnsafeMutablePointer(to: &hostInfo) {
$0.withMemoryRebound(to: integer_t.self, capacity: Int(size)) {
host_info(host_port, Int32(HOST_BASIC_INFO), $0, &size)
}
}
print(status1, hostInfo)
// THE FIX:
var pageSize: vm_size_t = vm_size_t()
let hostPageSizeKernStatus: kern_return_t = host_page_size(host_port, &pageSize)
var vm_stat = vm_statistics_data_t()
let HOST_VM_INFO_COUNT = MemoryLayout<vm_statistics_data_t>.stride/MemoryLayout<integer_t>.stride
var size2 = mach_msg_type_number_t(HOST_VM_INFO_COUNT)
let status2 = withUnsafeMutablePointer(to: &vm_stat) {
$0.withMemoryRebound(to: integer_t.self, capacity: Int(size2)) {
host_statistics(host_port, Int32(HOST_VM_INFO), $0, &size2)
//C:
// (void) host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
}
}
print(status2, vm_stat)
let freePages = UInt64(vm_stat.free_count)
let result = freePages * UInt64(pageSize)
return result
}
O my macBook I got:
47483648, cpu_type: 7, cpu_subtype: 8, cpu_threadtype: 1, physical_cpu: 4, physical_cpu_max: 4, logical_cpu: 8, logical_cpu_max: 8, max_mem: 17179869184)
vm_statistics(free_count: 1240186, active_count: 1765236, inactive_count: 124649, wire_count: 684265, zero_fill_count: 921561165, reactivations: 4645359, pageins: 19628659, pageouts: 23555, faults: 1940619345, cow_faults: 94990152, lookups: 2563634, hits: 40, purgeable_count: 166562, purges: 4217152, speculative_count: 306146)
and the result is : 5079801856 ie 5GB
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.