赞
踩
项目开发有一段时间了,一直没有时间检测内存情况,今天检测了一下内存,综合了几种情况
(1) 可以使用系统自带的leaks,在测试app的时候打开leaks,很准确哦
(2)也可使用第三方,可以检测某个UIViewController和UIView中内存泄漏,推荐使用轻量级的内存泄漏检测工具。
1、MLeaksFinder
介绍:MLeaksFinder:精准 iOS 内存泄露检测工具
2、PLeakSniffer
介绍:iOS内存泄漏自动检测工具PLeakSniffer
@interface MyViewController: UIViewController
//1.MyViewController 中强引用 childView
@property (nonatomic, strong) ChildView *childView;
@end
-----------------------------------------------------
@interface ChildView : UIView
@property (nonatomic, strong) MyViewController *controller;
-(instancetype)initWithController:(MyViewController *)controller;
@end
//implement
-(instancetype)initWithController:(MyViewController *)controller{
self = [super init];
if(self){
//2.这一步让View,强引用 MyViewController
self.controller = controller;
}
return self;
}

如上代码所示,1,2中让两个对象双向强引用,导致两者都不会被释放。
解决方案:
把2这一步换成weak
@property (nonatomic, weak) MyViewController *controller;
还是引用上面的案例,这次我们改成delegate版本
@interface MyViewController: UIViewController
//1.MyViewController 中强引用 childView
@property (nonatomic, strong) ChildView *childView;
@end
//
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。