如何以编程方式找到iOS应用程序的堆内存?

时间:2019-07-16 06:33:18

标签: java ios api swift3 swift4

如何快速查找iOS应用程序的应用程序特定堆内存?

是否有任何可能的方法可以使用应用程序ID或任何API查找堆内存。我试图使用下面的代码进行查找,但它与Xcode仪器的结果不匹配。

func memoryFootprint() -> mach_vm_size_t? {
    let TASK_VM_INFO_COUNT = mach_msg_type_number_t(MemoryLayout<task_vm_info_data_t>.size / MemoryLayout<integer_t>.size)
    let TASK_VM_INFO_REV1_COUNT = mach_msg_type_number_t(MemoryLayout.offset(of: \task_vm_info_data_t.min_address)! / MemoryLayout<integer_t>.size)
    var info = task_vm_info_data_t()
    var count = TASK_VM_INFO_COUNT
    let kr = withUnsafeMutablePointer(to: &info) { infoPtr in
        infoPtr.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { intPtr in
            task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), intPtr, &count)
        }
    }
    guard
        kr == KERN_SUCCESS,
        count >= TASK_VM_INFO_REV1_COUNT
        else { return nil }
    print((Double(info.internal)/Double(info.page_size))/1024)
    print((Double(info.resident_size)/Double(info.page_size))/1024)
    print((Double(info.phys_footprint)/Double(info.page_size))/1024)

    return info.phys_footprint
}

0 个答案:

没有答案