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<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 }

return info.phys_footprint

}

Replies

Did you read this:

https://stackoverflow.com/questions/48990831/getting-memory-usage-live-dirty-bytes-in-ios-app-programmatically-not-resident


Seems you have to add several values of task_vm_info_data_t to get the right result (

info.internal + info.compressed
)

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

Indeed. But what are you expecting to do with this value? The question of how much memory your app is using is a very subtle one, and there can be lots of different answers depending on how you measure. It’s hard to offer advice here without knowing what you intend to do with the resulting value.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Its matching what Xcode Debug navigator is showing, but I need the result as in xcode instrument, which is very much less than what we get in Xcode debug navigator.

Well I'm using the resulting value for performance testing purpose, I need to get the heap memory that matches with the Xcode instrument value.

I'm using the resulting value for performance testing purpose

Cool. That’s one of the few valid uses for this info.

I need to get the heap memory that matches with the Xcode instrument value.

Instruments shows lots of different memory values. Which one do you want specifically?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

In Instrument I want All Heap and Anonymous VM values ( from Allocations ). How to get it programatically?

One more doubt Iam having is that like in android by using package id and adb tool, cpu and memory usage can be found , same way is it possible in iOS? using bundle id or app id.

In Instrument I want All Heap and Anonymous VM values ( from Allocations ). How to get it programatically?

You can get heap allocations using

malloc_zone_statistics
. With regards the Anonymous VM values, it’s likely that you can get these using some low-level Mach statistic, but I don’t have an immediate answer for you off the top of my head.

One more doubt Iam having is that like in android by using package id and adb tool, cpu and memory usage can be found, same way is it possible in iOS? using bundle id or app id.

Yeah, I saw your other thread about this and I’m not sure how it’s different from this thread. Are you trying to get performance statistics for other applications? If so, that’s not possible on iOS. OTOH, if you’re trying to get these statistics for your own app, I don’t see how this new question is any different from the question that started the current thread.

ps I moved your thread to Core OS > Processes because this has nowt to do with Swift.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Using malloc_zone_statistics , heap memory of iOS app in obj-c can be found as shown below.

     #import <malloc/malloc.h>
malloc_statistics_t stats;
malloc_zone_statistics(NULL, &stats);
printf("%zu",stats.size_allocated);


How to use the same in swift? when I do the same in swift as :

var stats: malloc_statistics_t;

malloc_zone_statistics(nil, stats);
print(stats.size_allocated);


I get the error as - Cannot convert value of type 'malloc_statistics_t' to expected argument type 'UnsafeMutablePointer?'. Is this a right way?

The following file will help you.
In git repo of Chromium:
  • /refs/heads/master/ios/chrome/browser/memory/memory_metrics.h

  • /refs/heads/master/ios/chrome/browser/memory/memory_metrics.c