5

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!

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

0
- (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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.