简体   繁体   English

自动释放池

[英]autorelease pool

I was hoping someone could help me with a memory issue on the iPhone.我希望有人可以帮助我解决 iPhone 上的 memory 问题。

I have a scrollview that is part of a nav controller.我有一个滚动视图,它是导航 controller 的一部分。

Whenever I first push the nav controller and scroll through the scrollview (adding images as I go), memory is allocated in huge chunks (say 1mb each).每当我第一次按下导航 controller 并滚动浏览滚动视图(随我添加图像)时,memory 被分配成大块(比如每个 1mb)。

If I rotate the display a couple of times, the memory is freed, and everything is fine: The scrollview works correctly and the memory returns to where it should be (around 1mb, looking at the Net alloc in Instruments).如果我旋转显示器几次,memory 被释放,一切都很好:滚动视图正常工作,memory 返回到它应该在的位置(大约 1mb,查看仪器中的 Net alloc)。

How can I keep the memory from going wild, or free it up during use of the scrollview, just as the device rotation does?如何防止 memory 变得疯狂,或者在使用滚动视图期间释放它,就像设备旋转一样?

Here is the code snippet that is called to load the scrollview page:这是加载滚动视图页面的代码片段:

- (void)loadPage:(int)page isCurrent:(BOOL)isCurrent {
if (page < 0) return;
if (page >= totalRows) return;
picViewController *controller = [[picViewController alloc] init];
if ((NSNull *)[viewControllers objectAtIndex:page] == [NSNull null]) {
        NSString *fullPath = [self fullPath];
    if([fileManager fileExistsAtPath:fullPath]) {
        currentImage=[UIImage imageWithContentsOfFile:fullPath];
        [controller setImg:currentImage];
        [viewControllers replaceObjectAtIndex:page withObject:controller];
    }
    else {
            AsyncImageView* asyncImage = [[AsyncImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
            asyncImage.tag = 8;
            [asyncImage loadImageFromName:imageName withURL:url];
            [controller.view addSubview:asyncImage];
            [asyncImage release];
            [viewControllers replaceObjectAtIndex:page withObject:controller];
        }
        if (nil == controller.view.superview) {
            CGRect frame = scrollView.frame;
            frame.origin.x = frame.size.width * page;
            frame.origin.y = 0;
            controller.view.frame = frame;
            [scrollView addSubview:controller.view];
        }
    [controller release];
    }

} }

does your controller pay attention to memory low notifications?您的 controller 是否注意 memory 低通知? You could flush any images which aren't visible when that's received, or do any other releasing you think relevant.您可以刷新任何在收到时不可见的图像,或者进行您认为相关的任何其他发布。

I don't see where in your code you're actually purging off-screen pages.我看不到您在代码中实际清除屏幕外页面的位置。

Your code is somewhat like Apple's PageControl example, right?您的代码有点像 Apple 的PageControl示例,对吗?

What I do is loop through the pages each time a page is "activated" and purge pages more than 1 or 2 screens away.我所做的是每次“激活”页面时循环浏览页面并清除超过 1 或 2 个屏幕之外的页面。 (1 for without bounce scrolling, 2 for with.) (1 表示没有反弹滚动,2 表示有。)

- (void)updateCachedPages {
    int active = pageControl.currentPage;
    int count = pageControl.numberOfPages;
    for ( int i = 0; i < count; i++ ) {
        if ( abs( active - i ) <= 2 ) {
            [self loadPage:i];
        }
        else {
            [self purgePage:i];
        }
    }
}

- (void)purgePage:(int)page {
    if ((page < 0) || (page >= [_myObjects count])) return;

    MyControllerClass *controller = [_viewControllers objectAtIndex:page];
    if ((NSNull *)controller != [NSNull null]) {
        [_viewControllers replaceObjectAtIndex:page withObject:[NSNull null]];
        //NSLog( @"Purged page %d", page );
    }
}

Call updateCachedPages in scrollViewDidEndDecelerating , your PageControl's page change action, and viewWillAppear .scrollViewDidEndDecelerating中调用updateCachedPages 、PageControl 的页面更改操作和viewWillAppear

OK, I wasn't freeing all the memory after all.好吧,毕竟我并没有释放所有的 memory。

Once I found a class instance not being freed properly, and made sure tewha's purge code was working, everything is golden.一旦我发现一个 class 实例没有被正确释放,并确保 tewha 的清除代码正常工作,一切都是金色的。

Thanks for the quick help.感谢您的快速帮助。 This forum is awesome BTW.顺便说一句,这个论坛很棒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM