简体   繁体   English

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

[英]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.

相关问题 Xcode中的致命错误-我知道原因,但不知道如何解决 - Fatal error in Xcode - I know the cause but don't know how to fix it 如何修复Interface Builder Xcode 8.1中的错误? - How fix bug in interface builder xcode 8.1? xcode-我想接收设备语言 - xcode - I want to receive the device language 我正在研究xcode中的firebase,但是我想知道如何区分登录 - I'm studying firebase in xcode, but I want to know how to distinguish login 如何修复 Xcode 错误:无法使用 Xcode 在 iPhone 上找到设备支持文件 - How to fix Xcode error: Could not locate device support files on iPhone with Xcode 如何访问iOS设备(iPhone / iPad)的内存统计信息? - How to access iOS device (iPhone/iPad) memory statistics? 我是否必须在Xcode iOS项目中释放C的内存分配? - Do I have to free the memory alloc by C in Xcode iOS project? 如何释放AudioFileReadPacketData使用的内存-Xcode显示仍在使用的内存 - how to free memory used by AudioFileReadPacketData - Xcode shows memory still in use 运行Xcode项目时如何修复IUnityGraphicsMetal错误 - How to fix IUnityGraphicsMetal error when I run Xcode project 我想知道为什么UIImagePickerController的裁剪方块有一个小偏移量,以及如何修复它 - I want to know why UIImagePickerController's crop square has a slight offset, and how to fix it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM