Resident memory vs live bytes

1.7k Views Asked by At

Assumption: I am working on an iPhone project using Cocos2d 2.0 with ARC (and use Instruments of XCode 4.5.2).

Short question: Why resident memory is much higher than live bytes?

I say this because:

using instruments: I do get low memory warnings and I run my Allocation tool and see on avarage 3/5 MB of live bytes. Then I get a peak (18MB) and then back to 3/5MB. The problem is that, whenever I keep going back and forwards from one scene to the other I do get low memory warnings.

enter image description here

Using resident memory console print

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    [self report_memory];
    [[CCDirector sharedDirector] purgeCachedData];
}

-(void) report_memory {
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));
}
}

I do get memory warnings and the prints are 48MB the first time, then 48MB again and then 66MB and then.. crash!

So I wonder, why do people say that I should worry only about Live Bytes?

In other words, assuming my App is the only one running (all the other ones are killed) can I say that having very low live bytes (ranging from 4MB to 20MB) does NOT imply that I will NOT receive low memory warnings?

1

There are 1 best solutions below

2
On

The short answer to your question is "yes". Low live bytes does not imply that you will not receive low memory warnings. I say that because I have, on a few different occasions, seen memory warnings go by at the same time that Instruments is insisting my app's live bytes are quite reasonable.

However the vast majority of the time low live bytes is a good indicator that your app is not using very much memory, and live bytes is the number to watch if you're looking for a memory spike or indirect evidence of a leak.

Resident memory is a measurement of the memory that has been allocated to your application and has not yet been reclaimed by the system, but some/most of the resident memory could be reclaimed by the system. Live bytes is memory allocated to your application that cannot currently be reclaimed by the system. So you should expect your app's resident memory to always be higher (frequently much higher) than its live bytes.