使用测试用例,通过iOS应用程序以编程方式获取最高内存使用情况
我正在使用以下代码来获取 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论