1

在我的 iOS 应用程序中,当我从 viewController A 到 B 时,它使用系统内存是可以的。

但问题是,当我从 viewController A 到 B 再回到 A 再到 B 时,这会不断堆积越来越多的内存。

为什么它在返回视图 A 时不释放所有使用的内存。

我正在使用自动释放池。我正在消除不再需要的变量。

注意:我在视图 B 中使用 GCD,但我也在释放 GCD 中的内存。

我不知道为什么会这样。或者当我在视图 A 中时,有没有办法完全卸载视图 B 使用的所有资源?

更新:

我没有从 B 回到 A。我只是使用后退按钮。

这是我从视图 A 的转场

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

@autoreleasepool {

PhotosCollectionViewController *photoCollection=[segue destinationViewController];
NSIndexPath *path=[self.tableView indexPathForSelectedRow];

// get the cell tag for the selected row
NSIndexPath *path1=[NSIndexPath indexPathForRow:[path row] inSection:0];

UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:path1];

[photoCollection setPassedID:[[elements objectAtIndex:path1.row] objectAtIndex:0]];
// set the title of the next view controller
[photoCollection setTitle:cell.textLabel.text];

photoCollection=nil;
path=nil;
path1=nil;

}

}

编辑

我在 viewDidLoad 中的视图 b 中使用的长时间运行的进程

photoQueue=dispatch_queue_create("com.prepare.photos", nil);
dispatch_async(photoQueue, ^{

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(goToBackground)
                                                 name:UIApplicationWillResignActiveNotification object:nil];


display_photos=[[NSMutableArray alloc] init];




// iterate through photos array and create MWPhoto object out of it

// init ns cache
//myCache=[[NSCache alloc] init];

    int j=0;

    for(j=0;j<[photos count];j++){
        //NSLog(@"filename %@",[[photos objectAtIndex:i] objectAtIndex:0]);

        MWPhoto *mw=[MWPhoto photoWithFilePath:[self.documentsDirectory stringByAppendingPathComponent:[[photos objectAtIndex:j] objectAtIndex:0]]];

        [display_photos addObject:mw];
        mw=nil;


    }

    //dispatch_release(photoQueue);

});
4

2 回答 2

0

回答我自己的问题。它堆积内存的原因是,当我从 sqlite 数据库中检索一些数据时,我关闭了 sqlite 连接。因此,继续使用新视图会重新打开数据库连接,这会导致内存使用过多。

所以最重要的是,不要关闭 sqlite 数据库连接。

于 2013-05-02T07:17:41.623 回答
0

Segues 加载视图控制器的新实例;因此,当您使用 segue 将表单视图控制器 A 导航到 B 时,使用 segue 是有意义的。

A_1 -> B_1

但是,如果您使用 segue 从 B 导航到 A,则您正在创建视图控制器 A 的新实例,而不是返回到视图控制器 A 的先前实例。

A_1 -> B_1 -> A_2

来回切换会导致:

A_1 -> B_1 -> A_2 -> B_2 -> A_3 -> ...

理想情况下,您想要的是:

A_1 -> B_1 -> A_1

(从 B_1 回到 A_1)

为此,您可能希望将此方法用于从视图控制器 B 导航到 A 的按钮。

- (IBAction)backButton:(UIButton *)sender
    {
        [self dismissViewControllerAnimated:YES completion:^(void){
            NSLog(@"View controller dismissed");
            // things to do after dismissing
        }];
}
于 2014-12-31T03:26:50.497 回答