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


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 +1
MyObject *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.
     [[ NSNotificationCenter defaultCenter] 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
[[ NSNotificationCenter defaultCenter] 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值