tags:

views:

514

answers:

4

I was hoping someone could help me with a memory issue on the iPhone.

I have a scrollview that is part of a nav 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).

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).

How can I keep the memory from going wild, or free it up during use of the scrollview, just as the device rotation does?

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];
 }

}

A: 

does your controller pay attention to memory low notifications? You could flush any images which aren't visible when that's received, or do any other releasing you think relevant.

Graham Lee
A: 

Thanks for the quick response!

Yes it does respond to DidReceiveMemoryWarning, and I tried the following:

release imageviews in the hidden sections of the scrollview, release and recreate the scrollview, troll the view's subviews releasing everything and recreating, rotating the display programmatically, and nothing frees this memory. The only two things that do are either an alertview (if I launch the alertview several times all the memory is freed) or the device rotation.

I assume CG is holding onto these views, but it would be nice to keep the memory where it should be at 1mb. I load around 20 images into the scrollview, around 30kb to 50kb each. The net alloc pool reaches 12mb when scrolling (which the iPhone chokes on).

Is there any way to flush the held memory or pool, as the device rotation seems to be doing?

jj
A: 

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?

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 for without bounce scrolling, 2 for with.)

- (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.

Steven Fisher
hello i need your to implement your updateCachedPages method
Rahul Vyas
A: 

OK, I wasn't freeing all the memory after all.

Once I found a class instance not being freed properly, and made sure tewha's purge code was working, everything is golden.

Thanks for the quick help. This forum is awesome BTW.

jj
You should close the question so it doesn't show up on the unanswered list.
Steven Fisher