【问题标题】:App crashing, low memory应用程序崩溃,内存不足
【发布时间】:2012-07-01 10:59:38
【问题描述】:

我正在开发一个简单的 iPad 应用程序(iOS 5.1.1 和 Lion 上的 XCode 4.3.3),它可以水平滚动图像。它使用延迟加载,但不幸的是,由于图像大约 3/4 的内存不足,它正在崩溃。我使用 XCode 分析器和 Instruments 来查找和修复任何泄漏,但它仍然崩溃。崩溃日志如下:

Incident Identifier: 91B65E89-4CCE-4CB5-975F-365D23D802E5
CrashReporter Key:   659f640ad55a7dfbaf0305305e0b8f0fb36a6c41
Hardware Model:      iPad1,1
OS Version:          iPhone OS 5.1.1 (9B206)
Kernel Version:      Darwin Kernel Version 11.0.0: Sun Apr  8 21:51:26 PDT 2012; root:xnu-1878.11.10~1/RELEASE_ARM_S5L8930X
Date:                2012-06-29 14:51:58 -0700
Time since snapshot: 49 ms

Free pages:        610
Active pages:      0
Inactive pages:    694
Throttled pages:   43266
Purgeable pages:   0
Wired pages:       18518
Largest process:   GBTL

Processes
     Name                 UUID                    Count resident pages
        GBTL <d26b776787493174ae14f454a11b746d>   31295 (jettisoned) (active)
 debugserver <2408bf4540f63c55b656243d522df7b2>     134
    installd <0f9e14173c503a8089f7f8cd0329a1a0>     403
         atc <1e5f2a595709376b97f7f0fa29368ef1>     605 (jettisoned)
notification_pro <373a488638c436b48ef0801b212593c4>      96
notification_pro <373a488638c436b48ef0801b212593c4>      99
notification_pro <373a488638c436b48ef0801b212593c4>      96
        afcd <c3cc9d594b523fd1902fb69add11c25d>     133
  MobileMail <eed7992f4c1d3050a7fb5d04f1534030>    1004 (jettisoned)
        ptpd <62bc5573db7a352ab68409e87dc9abb9>     356 (jettisoned)
mediaserverd <f03b746f09293fd39a6079c135e7ed00>     404 (jettisoned)
        iapd <0a747292a113307abb17216274976be5>     285 (jettisoned)
  DTMobileIS <1383b8ed7d373b59a0839e89ab947089>     899
notification_pro <373a488638c436b48ef0801b212593c4>      99
 dataaccessd <473ff40f3bfd3f71b5e3b4335b2011ee>     735 (jettisoned)
springboardservi <b74f5f58317031e9aef7e95744c816ca>     283
syslog_relay <b07876a121a432d39d89daf531e8f2bd>      62
notification_pro <373a488638c436b48ef0801b212593c4>      98
syslog_relay <b07876a121a432d39d89daf531e8f2bd>      66
notification_pro <373a488638c436b48ef0801b212593c4>      98
filecoordination <0f34c714b10d35318f0d445f1d953386>      98
        geod <976e1080853233b1856b13cbd81fdcc3>     127
absinthed.K48 <04b40efabeac392ba63cba7256b86bb0>      74
     notifyd <f6a9aa19d33c3962aad3a77571017958>     181
  aosnotifyd <8cf4ef51f0c635dc920be1d4ad81b322>     369
    BTServer <31e82dfa7ccd364fb8fcc650f6194790>     186
CommCenterClassi <041d4491826e3c6b911943eddf6aaac9>     274
 SpringBoard <c74dc89dec1c3392b3f7ac891869644a>    4878 (active)
  aggregated <a12fa71e6997362c83e0c23d8b4eb5b7>     356
        apsd <e7a29f2034083510b5439c0fb5de7ef1>     630
     configd <ee72b01d85c33a24b3548fa40fbe519c>     304
fairplayd.K48 <47deae2d0fb23b6594b0128353e8b48d>     115
   fseventsd <914b28fa8f8a362fabcc47294380c81c>     175
     imagent <9c3a4f75d1303349a53fc6555ea25cd7>     361
   locationd <cf31b0cddd2d3791a2bfcd6033c99045>     833
mDNSResponder <86ccd4633a6c3c7caf44f51ce4aca96d>     188
mediaremoted <327f00bfc10b3820b4a74b9666b0c758>     201
   lockdownd <b06de06b9f6939d3afc607b968841ab9>     216
      powerd <133b7397f5603cf8bef209d4172d6c39>     133
     syslogd <7153b590e0353520a19b74a14654eaaa>      97
       wifid <3001cd0a61fe357d95f170247e5458f5>     258
UserEventAgent <dc32e6824fd33bf189b266102751314f>     362
     launchd <5fec01c378a030a8bd23062689abb07f>     123

**End**

这里是相关代码:

#import "GBTLViewController.h"

@implementation GBTLViewController

@synthesize scrollView;
@synthesize pageControl;
@synthesize pageImages;
@synthesize pageViews;

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self loadVisiblePages];

    if (!pageControlBeingUsed) {
        // Update the page when more than 50% of the previous/next page is visible
        CGFloat pageWidth = self.scrollView.frame.size.width;
        int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        self.pageControl.currentPage = page;
    }
}

- (IBAction)changePage {
    // update the scroll view to the appropriate page
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    [self.scrollView scrollRectToVisible:frame animated:YES];

    pageControlBeingUsed = YES;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    pageControlBeingUsed = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    pageControlBeingUsed = NO;
}

- (void)loadVisiblePages {
    // First, determine which page is currently visible
    CGFloat pageWidth = self.scrollView.frame.size.width;
    NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));

    // Update the page control
    self.pageControl.currentPage = page;

    // Work out which pages you want to load
    NSInteger firstPage = page - 1;
    NSInteger lastPage = page + 1;

    // Purge anything before the first page
    for (NSInteger i=0; i<firstPage; i++) {
        [self purgePage:i];
    }

    // Load pages in our range
    for (NSInteger i=firstPage; i<=lastPage; i++) {
        [self loadPage:i];
    }

    // Purge anything after the last page
    for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
        [self purgePage:i];
    }
}

- (void)loadPage:(NSInteger)page {
    if (page < 0 || page >= self.pageImages.count) {
        // If it's outside the range of what you have to display, then do nothing
        return;
    }

    UIView *pageView = [self.pageViews objectAtIndex:page];
    if ((NSNull*)pageView == [NSNull null]) {

        CGRect frame = self.scrollView.bounds;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0.0f;

        UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
        newPageView.contentMode = UIViewContentModeScaleAspectFit;
        newPageView.frame = frame;
        [self.scrollView addSubview:newPageView];
        [newPageView release];

        [self.pageViews replaceObjectAtIndex:page withObject:newPageView];
    }
}

- (void)purgePage:(NSInteger)page {
    if (page < 0 || page >= self.pageImages.count) {
        // If it's outside the range of what you have to display, then do nothing
        return;
    }

    // Remove a page from the scroll view and reset the container array
    UIView *pageView = [self.pageViews objectAtIndex:page];
    if ((NSNull*)pageView != [NSNull null]) {
        [pageView removeFromSuperview];
        [self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
    }
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a     nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    pageControlBeingUsed = NO;

    scrollView.delegate = self;
    [self.scrollView setBackgroundColor:[UIColor blackColor]];
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scrollView.clipsToBounds = NO;
    scrollView.scrollEnabled = YES;
    scrollView.pagingEnabled = NO;

    self.pageImages = [NSArray arrayWithObjects:
                   [UIImage imageNamed:@"480X_cover.jpg"],
                   [UIImage imageNamed:@"GBTL1.jpg"],
                   [UIImage imageNamed:@"GBTL2.jpg"],
                   [UIImage imageNamed:@"GBTL3.jpg"],
                   [UIImage imageNamed:@"GBTL4.jpg"],
                   [UIImage imageNamed:@"GBTL5.jpg"],
                   [UIImage imageNamed:@"GBTL6.jpg"],
                   [UIImage imageNamed:@"GBTL7.jpg"],
                   [UIImage imageNamed:@"GBTL8.jpg"],
                   [UIImage imageNamed:@"GBTL9.jpg"],
                   [UIImage imageNamed:@"GBTL10.jpg"],
                   [UIImage imageNamed:@"GBTL11.jpg"],
                   [UIImage imageNamed:@"GBTL12.jpg"],
                   [UIImage imageNamed:@"GBTL13.jpg"],
                   [UIImage imageNamed:@"GBTL14.jpg"],
                   [UIImage imageNamed:@"GBTL15.jpg"],
                   [UIImage imageNamed:@"GBTL16.jpg"],
                   [UIImage imageNamed:@"GBTL17.jpg"],
                   [UIImage imageNamed:@"GBTL18.jpg"],
                   [UIImage imageNamed:@"GBTL19.jpg"],
                   [UIImage imageNamed:@"GBTL20.jpg"],
                   nil];

    NSInteger pageCount = self.pageImages.count;

    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = pageCount;

    self.pageViews = [[[NSMutableArray alloc] init] autorelease];
    for (NSInteger i = 0; i < pageCount; ++i) {
        [self.pageViews addObject:[NSNull null]];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    CGSize pagesScrollViewSize = self.scrollView.frame.size;
    self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);

    [self loadVisiblePages];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    NSInteger pageCount = self.pageImages.count;

    for (NSInteger i=0; i<pageCount; i++) {
    [self purgePage:i];
    }

    CGSize pagesScrollViewSize = self.scrollView.frame.size;
    self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * pageCount, pagesScrollViewSize.height);

    [self loadVisiblePages];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.scrollView = nil;
    self.pageControl = nil;
}

- (void)dealloc {
    [scrollView release];
    [pageControl release];
    [super dealloc];
}

@end

如果有人能弄清楚内存问题发生在哪里以及如何解决,我将不胜感激。谢谢!

【问题讨论】:

  • 我没有答案,但请注意,使用 Instruments 检查内存分配可能会提供线索。
  • 你试过开启僵尸对象吗?
  • 将所有 UIImage 加载到数组中会消耗大量内存。只需用图像名称填充该数组,然后让loadPage 使用imageNamed 设置图像属性。这将解决眼前的问题。然后我会通过仪器运行它,看看你是否有泄漏。
  • 好的,我用 NSStrings 而不是 UIImages 加载了数组,并将它们加载到 loadPage() 中,而不是像建议的那样。但是,我仍然收到内存不足的错误。我应该注意,我正在第一代 iPad 上对此进行测试,所以我不确定在这些设备上可以使用多少内存和应用程序。使用模拟器和活动监视器,看起来应用程序一直在使用 40.3 MB。 Instruments 没有显示内存泄漏。
  • 你不能像那样创建 UIImage 并持有对它们的引用。相反,创建然后动态释放。不仅是视图,还有你在数组中持有的 UIImage 实例。 UIImage ref 是占用大块内存的东西,43 megs 实在是太大了。

标签: iphone objective-c ios ipad


【解决方案1】:

不知道这些是什么尺寸的图片,但有很多策略可以处理这种情况:

1) 你真的需要将图像加载到 NSArray 中吗?似乎根据您的文件命名方案,您可以构建一些逻辑以根据需要将其从包中拉出,然后在屏幕外立即丢弃。

2) 有时在 @autorelease 块中执行此类操作是有利的。看这里: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsautoreleasepool_Class/Reference/Reference.html

3) 您可能想查看以下示例应用程序缩小大图像以适应较低分辨率的屏幕: http://developer.apple.com/library/ios/#samplecode/LargeImageDownsizing/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011173

【讨论】:

    【解决方案2】:

    那里有相当多的图像。也许将图像的 NSString 名称存储在一个数组中,并且屏幕上只有一两个图像处于活动状态。

    【讨论】:

      【解决方案3】:

      一个问题是您的 loadPage 和 purgePage 方法中的以下两行:

      if ((NSNull*)pageView == [NSNull null]) {
      if ((NSNull*)pageView != [NSNull null]) {
      

      右侧 ([NSNull null]) 创建了一个新对象,您将 pageView 与该对象的地址进行比较。即使pageView 是另一个使用[NSNull null] 创建的对象,它的地址也很可能与您在if 条件中创建的新对象不匹配。最好使用nil 来指定尚未加载的页面。您可以通过将pageViews 字段设置为NSMutableDictionary(使用NSUInteger 作为键类型)轻松完成此操作。除此之外,您还可以将这些检查替换为以下类型的检查:

      if ([pageView isTypeOfClass:[NSNull class]]) {
      if (![pageView isTypeOfClass:[NSNull class]]) {
      

      【讨论】:

      • 我会将“不保证匹配 nil”扩展为“保证不匹配 nil”。 NSNull 是一个类,用于在不允许空值的地方表示空数据,例如 NSArrays。这在使用不同规则的语言之间解析数据时很有用;例如,我经常从解析的 JSON 数据中获取 NSNulls。
      • 我尝试了 if ([pageView isTypeOfClass:[NSNull null]]) 并收到“未找到实例方法”警告。我会试着弄清楚你提到的 NSMutableDictionary 方法。
      • @user1492253 哎呀!对不起。我不小心在上面输入了错误的内容。应该是[NSNull class]。我刚刚对其进行了编辑以解决该问题。
      猜你喜欢
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 2011-04-29
      • 2013-05-24
      • 2013-06-15
      • 1970-01-01
      相关资源
      最近更新 更多