使用测试用例,通过iOS应用程序以编程方式获取最高内存使用情况

发布于 2025-01-18 12:29:22 字数 1511 浏览 3 评论 0原文

我正在使用以下代码来获取 swift 5 中应用程序的内存使用情况。但我想显示测试用例内应用程序使用的最高内存,结果应与调试导航器内的“内存结果”相同。

func memoryFootprint() -> Float? {
        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 }
        
        let usedBytes = Float(info.phys_footprint)
        let usedMegaBytes = Double(usedBytes) / 1024 / 1024
        return Float(usedMegaBytes)
    }

  func getContinousMomery() {
        let currentMemory = Memory.memoryFootprint()
        
        if currentMemory! > heighMemory! {
            heighMemory = currentMemory
        }
        print("Heighest memory usage is: \(String(describing: heighMemory))")
    }

我能够从测试用例内的上述函数获取内存,但无法获取准确的最高内存。

我尝试在计时器内调用函数。但在测试用例中调用计时器内部的函数无法按预期工作。我在 setUp() 方法中有调度计时器,但每次调用新测试用例时都会调用设置方法。

我想要以编程方式获得最高内存的方法。

I am using following code to get memory usage by application in swift 5. But I want to display the highest memory used by application inside test cases and the result should be same as "memory result" inside debug navigator.

func memoryFootprint() -> Float? {
        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 }
        
        let usedBytes = Float(info.phys_footprint)
        let usedMegaBytes = Double(usedBytes) / 1024 / 1024
        return Float(usedMegaBytes)
    }

  func getContinousMomery() {
        let currentMemory = Memory.memoryFootprint()
        
        if currentMemory! > heighMemory! {
            heighMemory = currentMemory
        }
        print("Heighest memory usage is: \(String(describing: heighMemory))")
    }

I am able to get memory from above function inside test cases but not able to get accurate highest memory.

I tried calling function inside timer. But calling function inside timer in test cases not working as expected. I have schedule timer inside setUp() method, but setup method is called every time when new test case get called.

I want the way for getting highest memory programmatically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文