How to get memory usage in Swift

I need to get memory usage (MB) for my application programmatically in Swift. I found sample code for Objective-C. Anyone can help me convert this to Swift. Thanks!

void report_memory(void) { struct task_basic_info info; mach_msg_type_number_t size = sizeof(info); kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size); if( kerr == KERN_SUCCESS ) { NSLog(@"Memory in use (in bytes): %u", info.resident_size); } else { NSLog(@"Error with task_info(): %s", mach_error_string(kerr)); } } 
+7
ios iphone swift
source share
2 answers

edit: oops, had to check for duplicates first, Nate answered this question already here . Although you may notice a slight difference in my version, which uses withUnsafeMutablePointer .

Youre getting downvoted because people donโ€™t like โ€œHow can I convert this Obj-C code to Swiftโ€, but in this case this may be true, since this particular conversion is especially hairy. I think this works:

 func report_memory() { var info = task_basic_info() var count = mach_msg_type_number_t(sizeofValue(info))/4 let kerr: kern_return_t = withUnsafeMutablePointer(&info) { task_info(mach_task_self_, task_flavor_t(TASK_BASIC_INFO), task_info_t($0), &count) } if kerr == KERN_SUCCESS { println("Memory in use (in bytes): \(info.resident_size)") } else { println("Error with task_info(): " + (String.fromCString(mach_error_string(kerr)) ?? "unknown error")) } } 

A couple of basic constraints - sizeof returns an Int , but a function call requires UInt32 . Similarly (and a bit more without reason), TASK_BASIC_INFO is Int32 , but TASK_BASIC_INFO is required for the call.

The disgusting part takes place in the third parameter. task_info takes a pointer to several different types of structures of different sizes, depending on what information you want. Thus, you need to get a pointer from your TASK_BASIC_INFO object, and then apply it to a certain kind of pointer task_info really wants (which, as soon as you wade through all typedefs, a pointer to UInt32 ).

The docs for task_info say that the last count parameter should be considered the number of "the maximum number of integers in task_info", but when it says integers, I think it means that 32-bit integers are therefore divisible by 4.

+7
source share

Finally, I found a solution. See the code snippet below.

 func report_memory() { // constant let MACH_TASK_BASIC_INFO_COUNT = (sizeof(mach_task_basic_info_data_t) / sizeof(natural_t)) // prepare parameters let name = mach_task_self_ let flavor = task_flavor_t(MACH_TASK_BASIC_INFO) var size = mach_msg_type_number_t(MACH_TASK_BASIC_INFO_COUNT) // allocate pointer to mach_task_basic_info var infoPointer = UnsafeMutablePointer<mach_task_basic_info>.alloc(1) // call task_info - note extra UnsafeMutablePointer(...) call let kerr = task_info(name, flavor, UnsafeMutablePointer(infoPointer), &size) // get mach_task_basic_info struct out of pointer let info = infoPointer.move() // deallocate pointer infoPointer.dealloc(1) // check return value for success / failure if kerr == KERN_SUCCESS { println("Memory in use (in MB): \(info.resident_size/1000000)") } else { let errorString = String(CString: mach_error_string(kerr), encoding: NSASCIIStringEncoding) println(errorString ?? "Error: couldn't parse error string") } } 
+1
source share

All Articles