[英]How to fix this error(return host_statistics) in Xcode 8.1? I want to know the free memory of the device
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. 在返回host_statistics(host,HOST_VM_INFO,host_info_t($ 0),&count)的行中,出现错误:'init'不可用:使用'withMemoryRebound(to:capacity:_)'将内存临时查看为另一种与布局兼容的类型。
How to fix this? 如何解决这个问题?
for Swift 3.1. 适用于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: 我的MacBook,我得到了:
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) 47483648,cpu_type:7,cpu_subtype:8,cpu_threadtype:1,physical_cpu:4,physical_cpu_max:4,逻辑_cpu:8,逻辑_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) vm_statistics(免费计数:1240186,活动计数:1765236,不活动计数:124649,连线计数:684265,零填充计数:921561165,重新激活:4645359,调页:19628659,页面调出:23555,故障:1940619345,cow_faults:94990152,查找次数:2563 purgeable_count:166562,清除:4217152,speculative_count:306146)
and the result is : 5079801856 ie 5GB 其结果是:5079801856即5GB
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.