iOS 内存泄漏的几种原因

1、对象循环引用
@class ,Strong,weak

2、block循环引用
__weak typeof(self) weakself = self;

3、NSNotification的观察者忘记移除
[[NSNotificationCenter defaultCenter] removeObserver:self];

4、delegate循环引用问题
@property (nonatomic, weak) id delegate;

5、NSTimer循环引用
([NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeRepeat:) userInfo:nil repeats:YES]);
使用GCD

6、非OC对象内存处理
CGImageRef类型变量非OC对象,其需要手动执行释放操作CGImageRelease(ref),否则会造成大量的内存泄漏导致程序崩溃。其他的对于CoreFoundation框架下的某些对象或变量需要手动释放、C语言代码中的malloc等需要对应free等都需要注意;

7、使用过多的UIWebView
改为WKWebView

8、大次数循环内存暴涨问题
for (int i = 0; i < 100000; i++) {
NSString *string = @“Abc”;
string = [string lowercaseString];
string = [string stringByAppendingString:@“xyz”];
NSLog(@"%@", string);
}
改:
for (int i = 0; i < 100000; i++) {
@autoreleasepool {
NSString *string = @“Abc”;
string = [string lowercaseString];
string = [string stringByAppendingString:@“xyz”];
NSLog(@"%@", string);
}
}

9、加载大图片或者多个图片
[UIImage imageNamed:@""],次方法使用了系统缓存来缓存图像,会长时间占用内存,最好使用imageWithContentsOfFile方法;

10、ANF的AFHTTPSessionManager

11、地图类

猜你喜欢

转载自blog.csdn.net/heqiang2015/article/details/84575047