View不会完全释放

时间:2012-08-08 08:52:45

标签: ios memory-management uiviewcontroller

如果这是一个愚蠢的问题我很抱歉,但我对iOS开发很新。所以这是一个问题:

当我将SecondView添加到FirstView时,会分配内存,但是当我删除它时,并不会释放所有已分配的内存。

以下是代码:

的firstView:

@interface FirstView : UIViewController
@property (nonatomic, retain) SecondView *secondView;
- (IBAction)loadSecondView;
@end

@implementation ViewController
@synthesize editView;
- (IBAction)loadSecondView{
    secondView = [[SecondView alloc]init];
    [self presentModalViewController:editView animated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [secondView release]; secondView = nil; //this is called after SecondView is removed
}

SecondView:

@interface SecondView : UIViewController
@end

@implementation EditView
-(void)viewDidLoad{
    for (int intCounter = 0; intCounter < 10000; intCounter++){
        UIImageView *image = [[UIImageView alloc]init]; //create 10000 images just to fill up the memory
        [self.view addSubview:image];

        [image release]; image = nil;
    }
}
@end
  

以下是我从乐器(Live Bytes)获得的一些数字:

  1. 已加载FirstView: 671 KB
  2. 加载了SecondView: 3.28 MB
  3. 已删除SecondView: 809 KB
  4. 注意:仪器没有泄漏

    Live字节不应该成为初始值(671 KB)吗?而不是809 KB,还剩下什么?

1 个答案:

答案 0 :(得分:1)

这不是泄漏。 Apple缓存了一些细节。第一次加载viewcontroller时,它会增加内存使用量。一旦你解除了它,一些东西将保持缓存。

所以,第一读数不会给你准确的图片。

进行相同的运动2-3次。因此,您应该能够在第3次迭代开始时和第3次迭代结束时获得相同的内存利用率。

如果它在每个周期增加,则可能存在内存泄漏的情况。