Tuesday, June 21, 2011

Memory Problems

If you see the message "Received memory warning." in the debug logs you can check on the following links to first see what is happening when this low memory warning occurs.  They explain all about didReceiveMemoryWarning and viewDidUnload :



For starters you can nil out all of the view controller's IBOutlets in viewDidUnload and release any objects that can be recreated in viewDidLoad.

To altogether try to avoid low memory problems, you can try some of the tips in the link below.  The most effective one that worked for me is to not use UIImage imageNamed method so that the images are not cached.  Once cached you have no way of getting rid of it.  Not using that method saves a lot of space if you use a lot of images.  Below is some code that can be used to extend UIImage using categories.

In the header file:


#import
@interface UIImage (DoNotCache)
+ (UIImage *)newImageNotCached:(NSString *)filename;
@end


And in the implementation file use:


@implementation UIImage (DoNotCache)
+ (UIImage *)newImageNotCached:(NSString *)filename {
NSString *imageFile = [[NSString alloc] initWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], filename];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imageFile];
[imageFile release];
return image;
}
@end


More tips can be found here.

No comments:

Post a Comment