iOS开发iOS Developer

iOS开发中本人或同事碰到的内存泄漏及解决办法

2016-09-14  本文已影响5800人  helloDolin

首先需要理解两个概念:


好的,进入正题,我逐步演示碰到的各种内存泄漏:


round1:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSMutableArray *arr1 = [NSMutableArray array];
    NSMutableArray *arr2 = [NSMutableArray array];

    [arr1 addObject:arr2];
    [arr2 addObject:arr1];
}

我们用Xcode的Instruments工具检测一下

Paste_Image.png
上图中我们已经很明显的看到了循环引用
解决办法:打破环!
上述例子中将arr1或者arr2任意一个 弱引用就OK
贴张图出来与上面代码的不同会更加清晰(__)

再次用工具检测:


看到leak那行都是绿色的是不是很棒?

round2:

首先我们建两个model:Student、Teacher
Student类中有属性teacher
Teacher类中有属性student
并且都重写了dealloc方法,打印信息

#import <Foundation/Foundation.h>
#import "Student.h"

@interface Teacher : NSObject

@property (nonatomic, strong) Student *student;

@end

#import "Teacher.h"

@implementation Teacher

- (void)dealloc {
    NSLog(@"%s",__func__);
}

@end
#import <Foundation/Foundation.h>
@class Teacher;

@interface Student : NSObject

@property (nonatomic, strong) Teacher *teacher;

@end

#import "Student.h"
@implementation Student

- (void)dealloc {
    NSLog(@"%s",__func__);
}
@end

第一个页面push到第二个页面,第二个页面代码如下:

#import "ViewController2.h"
#import "Student.h"
#import "Teacher.h"

@interface ViewController2 ()
{
    Teacher *_teacher;
    Student *_student;
}
@end

@implementation ViewController2

// dealloc
- (void)dealloc {
    NSLog(@"%s",__func__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _student = [[Student alloc]init];
    _teacher = [[Teacher alloc]init];
    
    _student.teacher = _teacher;
    _teacher.student = _student;
    
}

运行push到第二页面在pop回第一个页面看控制台的打印信息

仅仅走了Controller的dealloc方法,两个model的dealloc都没有走

为什么呢?我们分析一下

觉得还是画图解释的快一点((⊙﹏⊙)虽然小编累一点)
如图:
上半部分是两个model的UML图,
下半部分我们看到_student实例的teacher属性引用了_teacher实例,而实例_teacher的student属性引用了_student实例这样就形成了一个引用环,由于OC的内存管理机制就导致了这两块内存不能被释放导致内存泄漏
解决办法:打破环!
我们将Student或Teacher类里的属性 任意一个内存语义strong改为weak,再跑一下看看结果:

三个dealloc方法都走了,是不是很nice?

round3:

最最常见的一种,block中的操作

typedef void(^TestBlock)(void);
@interface ViewController2 ()
@property (nonatomic, copy) TestBlock block;
@end

@implementation ViewController2

- (void)dealloc {
    NSLog(@"%s",__func__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.block = ^ {
        self.view.backgroundColor = [UIColor redColor];
    };
}

@end

block为什么用copy修饰(请百度,不在本次讨论重点内)
由于block会retain当前对象,所以这里也形成了一个环
vc2引用block,block保留当前对象self
解决办法:打破环
将block内的self弱引用就OK

round4:

上面3中情况都是循环引用造成的内存泄漏,解决办法都是打破环
ok,看如下代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [center addObserverForName:UIKeyboardWillShowNotification
                        object:nil
                         queue:mainQuene
                    usingBlock:^(NSNotification *note) {
                        self.view.backgroundColor = [UIColor redColor];
                    }];
}

当pop到上一页面的时候 dealloc 方法也没有走,why?
我们分析一下:
center -> block -> self
但是self有没有拥有center?答案是有的,具体里边的实现暂时我不清楚,但确定的是有环的存在

参考:http://stackoverflow.com/questions/12699118/view-controller-dealloc-not-called-when-using-nsnotificationcenter-code-block-me

解决办法:打破环
    __weak typeof(self) weakSelf = self;
    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [center addObserverForName:UIKeyboardWillShowNotification
                        object:nil
                         queue:mainQuene
                    usingBlock:^(NSNotification *note){
                        weakSelf.view.backgroundColor = [UIColor redColor];
                    }];

以上就是我碰到的或同事碰到的导致内存泄漏的几种情况,真正开发中碰到的应该都比较复杂,利用好Instruments加上开发时注意保持良好习惯,大都可以避免

其实这篇文章很早就想写,但是这边涉及的内容确实太多了
block为什么会retain当前对象?
OC为什么采用引用计数来管理内存?
管理内存语义的那些关键字(strong,weak,copy等)之间的差异
Instruments工具怎么灵活运用?
等等,所以本篇就只针对内存泄漏,其他我也不很熟,很多没想通,也不敢写

这下也就很容易理解为什么delegate要用weak修饰了(不明白的话留言,我再补个图上来)
so:到此结束!(一气呵成的感觉好爽O(∩_∩)O哈哈~)


希望会给大家带来帮助(o)/~

上一篇下一篇

猜你喜欢

热点阅读