驻留内存与活动字节 [英] Resident memory vs live bytes

查看:151
本文介绍了驻留内存与活动字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设:我正在使用带有Cocos2d 2.0和ARC的iPhone项目(并使用XCode 4.5.2的工具).

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:

使用工具: 我确实收到内存不足警告,并且运行分配工具并看到3/5 MB的活动字节.然后我达到一个峰值(18MB),然后又回到3/5MB.问题在于,每当我不断地从一个场景前进到另一个场景时,都会收到内存不足的警告.

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.

使用常驻内存控制台打印

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

我确实收到了内存警告,第一次打印的是48MB,然后是48MB,然后是66MB,然后..崩溃!

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?

换句话说,假设我的应用程序是唯一正在运行的应用程序(其他所有应用程序都被杀死了)我能说活动字节数非常低(从4MB到20MB)并不意味着我不会收到内存不足警告?

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?

推荐答案

您的问题的简短答案是是".低有效字节不会不是暗示您将不会收到低内存警告.我这么说是因为我在几次不同的情况下都看到内存警告同时出现,因为仪器正在坚持,我的应用程序的活动字节是相当合理的.

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.

这篇关于驻留内存与活动字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆