iOS避免内存警告的一些建议

来源:互联网 发布:万网域名指向花生壳 编辑:程序博客网 时间:2024/05/15 04:38

1、不要使用 UIImage imageNamed: method

不用:

#import <Foundation/Foundation.h>@interface UIImage (DoNotCache)+ (UIImage *)newImageNotCached:(NSString *)filename;@end

使用

@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

2、将View分成多个,不要在一个。

3、使用本地的 NSAutoRelease Pool

If you are autoreleasing lot of objects, you may run into situations where your objects may not be released until your program terminates. If so, think about using a local autorelease pool. Autorelease pools are staked, so if you create a new autorelease pool, it gets inserted into the top of the stack.

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];NSString *string = [[[NSString alloc] init] autorelease];[pool drain];

4、记得及时释放

NSMutableArray *array = [[NSMutableArray alloc] init]// obj has retain count of +1MyObject *obj = [[MyObject alloc] init]// obj has retain count of +2, so make sure to release it[array addObject:array];      // obj has retain count of +1[obj release];


http://www.uchidacoonga.com/2009/08/handling-didreceivememorywarning/

http://www.bdunagan.com/2008/10/01/triggering-didreceivememorywarning-on-the-iphone/

 

Now that the NDA is lifted, I can post a handy iPhone dev trick. Craig Hockenberry posted a twitter a while back recommending you trigger didReceiveMemoryWarning in your app to work out any memory issues. And being able to trigger that warning in the iPhone Simulator, rather than on the device, would make debugging easier.

However, getting your warning to propagate through your objects like a real warning does is a bit tricky. You have to use an undocumented notification message: UIApplicationMemoryWarningNotification. You can wrap it in a method like so.

1
2
3
4
5
6
7
- (void)triggerMemoryWarning
{
    //Post 'low memory' notification that will propagate out tocontrollers
    //Note: UIApplicationDidReceiveMemoryWarningNotification doesn't workfor some reason.
    [[NSNotificationCenterdefaultCenter] postNotificationName:
            @"UIApplicationMemoryWarningNotification"object:[UIApplication sharedApplication]];
}

Putting that method on a timer allows you to trigger a low memory warning throughout your application at a regular interval. For some reason, it doesn’t trigger the warning in your base controller, but you can easily add that class as an observer to that notification.

1
2
3
4
[[NSNotificationCenterdefaultCenter] addObserver:[[UIApplication sharedApplication]delegate]
                                                       selector:@selector(applicationDidReceiveMemoryWarning:)
                                                       name:@"UIApplicationMemoryWarningNotification"
                                                       object:nil];

This process should allow you to trigger a didReceiveMemoryWarning notification throughout your iPhone application just like a real warning. The fact that it’s undocumented is okay because it’s only for testing purposes.

原创粉丝点击