运行iOS应用程序的内存使用情况信息错误

时间:2018-09-14 07:13:49

标签: ios swift xcode memory

我从此门户网站获得了此代码,但是此答案未显示运行应用程序iOS应用程序的内存使用情况的正确值。如何在此答案中找到要更改的正确值?

func getMemory() {

    var taskInfo = mach_task_basic_info()
    var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size)/4
    let kerr: kern_return_t = withUnsafeMutablePointer(to: &taskInfo) {
        $0.withMemoryRebound(to: integer_t.self, capacity: 1) {
            task_info(mach_task_self_, task_flavor_t(MACH_TASK_BASIC_INFO), $0, &count)
        }
    }
    if kerr == KERN_SUCCESS {
        let usedMegabytes = taskInfo.resident_size/(1024*1024)
        print("used megabytes: \(usedMegabytes)")
    } else {
        print("Error with task_info(): " +
            (String(cString: mach_error_string(kerr), encoding: String.Encoding.ascii) ?? "unknown error"))
    }

}

使用以上答案,我得到:已用兆字节:125

以下是Xcode的屏幕截图:

enter image description here

0 个答案:

没有答案