如何使用ARC模式管理内存

时间:2013-02-07 05:58:01

标签: iphone objective-c automatic-ref-counting

我正在创建一个ios应用程序,我启用了ARC模式。 我正在向ScrollView加载24个实例,但在给定时间只显示三个视图。因此,我将删除已加载到内存中的其他不需要的实例

if((NSNull *)[viewControllers objectAtIndex:2] != [NSNull null]) {
    [viewControllers replaceObjectAtIndex:2 withObject:[NSNull null]];
    [content3.view removeFromSuperview];
    //remove third content page
    [content3 viewDidUnload];
    //set nil to instance to memory management
    content3 = nil;

    NSLog(@"Content 3 removed");
}

但是当我查看探查器时,内存不会被释放,只会自动增加。

有人可以给我一些关于这个问题的提示吗?

由于

1 个答案:

答案 0 :(得分:0)

我认为您的代码中存在很多问题:

  1. 一个重要问题是,您正在调用委托方法viewDidUnload。永远不要尝试做这些事情,否则你会从你的应用程序中面对奇怪的行为。
  2. 如果您需要从内存中删除对象,只需将其设置为nil。
  3. 我认为你的if条件永远不会有效,你为什么要输入NSNull?
  4. 检查此代码:

    if([viewControllers objectAtIndex:2] != [NSNull null])
    {
        [viewControllers replaceObjectAtIndex:2 withObject:[NSNull null]];
        [content3.view removeFromSuperview];
        content3 = nil;
        NSLog(@"Content 3 removed");
    }