Reputation: 7612
I would like to know how much memory my application is using. How would I be able to retrieve this programmatically? Is there an easy Cocoa way to do this, or would I have to go all the way down to C?
Thanks!
Upvotes: 5
Views: 1466
Reputation: 7612
- (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;
}
}
Upvotes: 0
Reputation: 27073
Working snippet:
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 don't know the original source anymore.
Upvotes: 7