Cocoa - How to programmatically get the amount of memory used by your application?

I would like to know how much memory my application is using. How can I get this programmatically? Is there an easy way for Cocoa to do this, or will I have to go all the way to C?

Thanks!

+4
source share
2 answers

Working fragment:

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

Unfortunately, I no longer know the source.

+7
source
 - (NSInteger)getMemoryUsedInMegaBytes { NSInteger memoryInBytes = [self getMemoryUsedInBytes]; return memoryInBytes/1048576; } - (NSInteger)getMemoryUsedInBytes { 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 ) { return info.resident_size; } else { return 0; } } 
0
source

All Articles