Programmatically retrieve memory usage on iPhone

前端 未结 9 935
迷失自我
迷失自我 2020-11-22 16:56

I\'m trying to retrieve the amount of memory my iPhone app is using at anytime, programmatically. Yes I\'m aware about ObjectAlloc/Leaks. I\'m not interested in those, only

9条回答
  •  醉梦人生
    2020-11-22 17:27

    Swift solution of Jason Coco's answer:

    func reportMemory() {
        let name = mach_task_self_
        let flavor = task_flavor_t(TASK_BASIC_INFO)
        let basicInfo = task_basic_info()
        var size: mach_msg_type_number_t = mach_msg_type_number_t(sizeofValue(basicInfo))
        let pointerOfBasicInfo = UnsafeMutablePointer.alloc(1)
    
        let kerr: kern_return_t = task_info(name, flavor, UnsafeMutablePointer(pointerOfBasicInfo), &size)
        let info = pointerOfBasicInfo.move()
        pointerOfBasicInfo.dealloc(1)
    
        if kerr == KERN_SUCCESS {
            print("Memory in use (in bytes): \(info.resident_size)")
        } else {
            print("error with task info(): \(mach_error_string(kerr))")
        }
    }
    

提交回复
热议问题