how to get iOS app specific heap memory usage in swift programatically?

I have used below code. It is matching with Xcode memory monitor but not matching with the Xcode instrument allocation result.

 

func memoryFootprint() -> mach_vm_size_t? {

    let TASK_VM_INFO_COUNT = mach_msg_type_number_t(MemoryLayout.size / MemoryLayout.size)

    let TASK_VM_INFO_REV1_COUNT = mach_msg_type_number_t(MemoryLayout.offset(of: task_vm_info_data_t.min_address)! / MemoryLayout.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 }

    return info.phys_footprint

}

Powered by WPeMatico

About

You may also like...

Comments are closed.