gpt4 book ai didi

ios - 如何在 Xcode 8.1 中修复此错误(返回 host_statistics)?我想知道设备的可用内存

转载 作者:行者123 更新时间:2023-11-28 12:37:18 26 4
gpt4 key购买 nike

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)
}

return host_statistics(host, HOST_VM_INFO, host_info_t($0), &count) 行中有一个错误:'init' 不可用:使用'withMemoryRebound(to:capacity:_)' 来暂时将内存视为另一种布局兼容类型。

如何解决这个问题?

最佳答案

适用于 Swift 3.1。 (我添加了更多代码来检查您可能需要的所有信息。)

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
}

我的 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)

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)

结果是:5079801856即 5GB

关于ios - 如何在 Xcode 8.1 中修复此错误(返回 host_statistics)?我想知道设备的可用内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40501398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com