我想跟踪我的应用的内存使用情况。我的代码并没有给我与Xcode相同的价值(在调试导航器视图中)
func report_memory() {
var info = mach_task_basic_info()
var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size)/4
let kerr: kern_return_t = withUnsafeMutablePointer(to: &info) {
$0.withMemoryRebound(to: integer_t.self, capacity: 1) {
task_info(mach_task_self_,
task_flavor_t(MACH_TASK_BASIC_INFO),
$0,
&count)
}
}
if kerr == KERN_SUCCESS {
let unit = UInt64(1024 * 1024)
let memUsage = info.resident_size / unit
print("Memory in use (in MB): \(memUsage)")
} else {
print("Error with task_info(): " +
(String(cString: mach_error_string(kerr), encoding: String.Encoding.ascii) ?? "unknown error"))
}
}
我的代码(在模拟器上运行): 139MB
Xcode: 84MB
你知道为什么吗?谢谢!