iOS内存泄漏检测
内存泄漏
通俗来说就是有一块内存区域被你占用了,但你又不使用这块区域也不让别人用,造成内存浪费,这就是内存泄漏,泄漏严重会造成内存吃紧,严重的会使程序崩溃。
1、造成泄漏的点
(1)引用循环(Retain Cycle),Block强引用
(2)NSTimer释放不当
(3)第三方提供方法造成的内存泄漏
(4)CoreFoundation方式申请的内存,忘记释放
2、监测方法
(1)逻辑错误:访问空指针或未初始化的变量等;
(2)内存管理错误:如内存泄漏等;
(3)声明错误:从未使用过的变量;
(4)Api调用错误:未包含使用的库和框架。
3、内存分类
(1)、Leaked memory(内存未被应用程序引用,不能再次使用或释放)
(2)、Abandoned memory(内存仍然被应用程序引用,但没有任何有用的用途)
(3)、Cached memory(内存仍然被应用程序引用,可能会再次用于提高性能)
4、检测工具
(1)Instruments Leaks
(2)MLeaksFinder
注意: Leaked memory 和 Abandoned memory 都属于应该释放而没释放的内存,都是内存泄露,其中Leaks工具只负责检测 Leaked memory,而不管 Abandoned memory,因此Leaks并不能检测出所有的内存泄漏。而MLeaksFinder 可以检测出循环引用导致的内存泄漏,使用时可以二者结合解决问题!
5、自定义检测工具
MLeaksFinder原理:
利用swizzle了NavigationController的Push和Pop相关方法来管理viewController和view的生命周期
实现思路:
在VC中设置一个属性,viewWillAppear的时候设置为NO,pop返回出栈的时候(暂时只考虑pop情况)设置为YES,我们在viewDidDisappear中,延迟访问这个这个属性,如果他为YES,那么就代表存在内存问题.
创建一个UIViewController的Categroy
constchar*XC_LEAKS_CHECK;
@implementationUIViewController (Leaks)
+ (void)load {
staticdispatch_once_tonceToken;
dispatch_once(&onceToken, ^{ [selfxc_swizzledOriginSEL:@selector(viewWillAppear:)swizzledSEL:@selector(xc_viewWillAppear:)]; [selfxc_swizzledOriginSEL:@selector(viewDidDisappear:)swizzledSEL:@selector(xc_viewDidDisappear:)];
});
}
+ (void)xc_swizzledOriginSEL:(SEL)originSEL
swizzledSEL:(SEL)swizzledSEL {
MethodoriginMethod =class_getInstanceMethod([selfclass], originSEL);
MethodswizzledMethod =class_getInstanceMethod([selfclass], swizzledSEL);
method_exchangeImplementations(originMethod, swizzledMethod);
}
- (void)xc_viewWillAppear:(BOOL)animated {
[self xc_viewWillAppear:animated];
objc_setAssociatedObject(self, XC_LEAKS_CHECK, @(NO), OBJC_ASSOCIATION_RETAIN);
}
- (void)xc_viewDidDisappear:(BOOL)animated {
[self xc_viewDidDisappear:animated];
if ([objc_getAssociatedObject(self, XC_LEAKS_CHECK) boolValue]) {
//发送消息
[selfwillDelloc];
}
}
- (void)willDelloc {
__weak typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
__strongtypeof(weakSelf) strongSelf = weakSelf;
[strongSelfisNotDelloc];
});
}
- (void)isNotDelloc {
NSLog(@"%s",__func__);
}
创建一个UINavigationController的Categroy
+ (void)load {
staticdispatch_once_tonceToken;
dispatch_once(&onceToken, ^{
[selfxc_swizzleOriginSEL:@selector(popViewControllerAnimated:)
swizzledSEL:@selector(xc_popViewControllerAnimated:)];
});
}
+ (void)xc_swizzleOriginSEL:(SEL)originSEL swizzledSEL:(SEL)swizzledSEL{
MethodoriginMethod =class_getInstanceMethod([selfclass], originSEL);
MethodcurrentMethod =class_getInstanceMethod([selfclass], swizzledSEL);
method_exchangeImplementations(originMethod, currentMethod);
}
- (UIViewController*)xc_popViewControllerAnimated:(BOOL)animated{
externconstchar*XC_LEAKS_CHECK;
UIViewController*popViewController = [selfxc_popViewControllerAnimated:animated];
objc_setAssociatedObject(popViewController, XC_LEAKS_CHECK, @(YES), OBJC_ASSOCIATION_ASSIGN);
returnpopViewController;
}
在vc中添加循环引用的代码
self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(testBlock) userInfo:nil repeats: YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
注意:不要在viewDidDisappear中释放
demo地址:https://github.com/709530753/LeaksTest