3

I am currently developing an iPhone game that has numerous animations using image sequences (jpg and png with transparencies). Using Instruments to view Leaks and Object Allocations, the most I've seen the object allocations go up to is less than 500k. However, the program still crashes and quits unexpectedly when we switch from one level of the game to another. Is this abnormal? From what I've read, the iPhone has 128Mb of memory.

More info: We are using CAKeyFrameAnimation (as adviced by Apple) and imageNamed (so that the images are pre-cached). In each Level object, I can see that the dealloc is getting called when I swap one level out and load another. However memory seems to keep going up and up and never gets released back down to its original level. =(

Ben X Tan
  • 287
  • 3
  • 10
  • I've noticed in Leaks that memory usage doesn't immediately go down after releasing a UIImage. Maybe there's an internal cache or something? I'm curious to figure this out as well. – Daniel Dickison Jun 03 '09 at 13:38
  • Yes the imageNamed command is supposed to cache the image, and that is our desired functionality. But dying at 500k doesn't seem right to me. Also it would be nice if the UIImages were completely cleared off after we've unloaded a level. How are you doing the animation in your code (if any). We were using the animationImages property of UIImageView, but were then told by Apple that it was buggy and never released images. They advised us to use CAKeyframeAnimation instead. – Ben X Tan Jun 03 '09 at 14:02
  • Actually I'm not doing animation -- just displaying rather large images downloaded from the internet. In fact, I'm creating the images with imageWithData:, not imageNamed:, so I'm surprised it's doing any caching in my case. – Daniel Dickison Jun 03 '09 at 14:11
  • Ah ok. How big are the images? kb and pixels... Ours vary from full iPhone screen size at approx 20kb(jpg) to 210kb (png) – Ben X Tan Jun 03 '09 at 14:31
  • They're in the range of a few hundred KB, ~1000x1000 pixels, though I think they get scaled to screen size when displayed in an image view inside a scroll view. I should note that I've never had any memory crashing issues with this. Is it possible you're looking in the wrong place? – Daniel Dickison Jun 03 '09 at 14:53
  • FIXED!!! Firstly, we switched to using [UIImage imageWithContentsOfFile] to load the images. Secondly, (the eureka moment!,) came when I put in an NSLog in the dealloc method of our ActorView (extends UIImageView) object. This is the important part. It was here that I realised that we weren't clearing off our actors properly and that was what was causing our memory leaks! We tried doing a release before but got errors, I believe because we were using a foreach loop with objectEnumerator, which we have since changed. – Ben X Tan Jun 04 '09 at 04:33
  • The code now looks like this: - (void) clearActors { for (ActorView* currentView in [[self view] subviews]) { [currentView.layer removeAllAnimations]; [currentView removeFromSuperview]; [currentView release]; } } – Ben X Tan Jun 04 '09 at 04:33
  • I am about to go through and check that ALL our other objects are being dealloced. Results: Basically, the game currently has 3 levels (all extends our Level object), Menu, Level 1, Level 2. I have cycled through all 3 levels 3 times, watching the "Real Memory" column in Instruments->Activity Monitor and the usage is very consistent. This was tested with v2.2.1. I'm about to test with v3.0 now. Going to try a bunch of other improvements we've been throwing around. – Ben X Tan Jun 04 '09 at 04:34

1 Answers1

0

you can use the autorelease for it and call an explicitly function to release the unused image view by which image allocation will be low and when the delloac function will be called you already have done some work by relaeseing them explicitly so when delloc call it will freeup mostly of the memory try this one.

thanks