iOS-Alloc对象,直到didReceiveMemoryWarning被调用 [英] iOS - Alloc objects until didReceiveMemoryWarning is called

查看:170
本文介绍了iOS-Alloc对象,直到didReceiveMemoryWarning被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这样做:分配对象,直到调用内存警告,然后释放所有对象.但是我有一些问题.我怎样才能做到这一点?我需要代码示例,因为问题是:代码.我有一个不使用ARC的课程.此类具有一种方法,该方法分配保存到数组中的N个对象.我需要在调用didReceiveMemoryWarning之前将内存填满,因为这是在iOS上释放" RAM内存的唯一方法.然后,我将释放所有.我认为App Store上用于iPhone的清洁内存应用程序使用此技巧来释放"内存. 预先感谢

I need to do this : alloc objects until memory warning is called and then release all objects. But I have some problems. How can I do this? I need code example because the problem is that : the code. I have a class that doesn't use ARC. This class has a method which alloc N objects that are saved into an array. I need the memory is filled until didReceiveMemoryWarning is called because this is the only method to "free" RAM memory on iOS. Then, I will release all. I think the cleaner memory apps for the iPhone on the App Store use this trick to "free" the memory. Thanks in advance

推荐答案

您必须填写缺少的详细信息,但这是我以前使用的内容.功劳归功于我在任何地方找到它的人.这将适用于ARC和非ARC项目.我发现通常在完全死亡之前会收到2-3条警告.祝你好运.晚餐长度是每次分配多少块.如果您想要更细粒度的内存控制,请更改大小.

You'll have to fill in the missing details but here is what I have used before. Credit goes to who/where ever I found it. This will work on ARC and non ARC projects. I have found that usually you get 2-3 warnings before you're completely dead. Good luck. Dinner length is how much of a chunk that gets allocated each time. if you want more fine grained memory control change the size.

-(IBAction)startEatingMemory:(id)sender
{
    if(self.belly == nil){
        self.belly = [NSMutableArray array];
    }
    self.paused = false;
    [self eatMemory];
} 

- (IBAction)pauseEat:(id)sender {
    self.paused = true;
    [[self class]cancelPreviousPerformRequestsWithTarget:self selector:@selector(eatMemory) object:nil];
}

- (IBAction)stopEatingMemory:(id)sender {
    [self pauseEat:self];

    [self.belly removeAllObjects];
    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(eatMemory) object:nil];

 }


-(void)eatMemory
{
    unsigned long dinnerLength = 1024 * 1024;
    char *dinner = malloc(sizeof(char) * dinnerLength);
    for (int i=0; i < dinnerLength; i++)
    {
        //write to each byte ensure that the memory pages are actually allocated
        dinner[i] = '0';
    }
    NSData *plate = [NSData dataWithBytesNoCopy:dinner length:dinnerLength freeWhenDone:YES];
    [self.belly addObject:plate];


    [self performSelector:@selector(eatMemory) withObject:nil afterDelay:.1];
}



-(void)didReceiveMemoryWarning 
{

    [self pauseEat:self];
    <#Could release all here#>
    [super didReceiveMemoryWarning];
}

这篇关于iOS-Alloc对象,直到didReceiveMemoryWarning被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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