【iOS内存调试】Debug Memory Graph
2021-05-14 本文已影响0人
文子飞_
1、作用
可在程序运行时,排查和定位内存泄漏问题。
2、打开Malloc stack logging
打开此设置可以backtrace对象堆栈信息。
![](https://img.haomeiwen.com/i23280551/cd41e22dc283c583.png)
3、排查循环引用
下面代码存在循环引用问题。导致内存不释放。
@implementation ThirdVC
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSMutableArray *arr1 = [NSMutableArray array];
NSMutableArray *arr2 = [NSMutableArray array];
[arr1 addObject:arr2];
[arr2 addObject:arr1];
}
- (void)dealloc {
NSLog(@"ThirdVC dealloc");
}
@end
![](https://img.haomeiwen.com/i25012095/f501671d68dc34e5.jpeg)
![](https://img.haomeiwen.com/i25012095/ddbdc99bb46fd300.jpeg)
4、排查更隐秘的循环引用
下面代码存在定时器循环引用问题。导致内存不释放。
但是,这种内存泄漏问题,通过3方式,并不会排查出来。
#import "SecondVC.h"
@interface SecondVC ()
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation SecondVC
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)dealloc {
NSLog(@"SecondVC dealloc");
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}
- (void)timerAction {
NSLog(@"TimerAction");
}
@end
操作步骤:
1、HomeVC push SecondVC,点击屏幕开启定时器,然后返回。
2、HomeVC push SecondVC,点击屏幕开启定时器,然后返回。
3、HomeVC push SecondVC,点击屏幕开启定时器。
![](https://img.haomeiwen.com/i25012095/e4a42d43dcf2523c.jpeg)
![](https://img.haomeiwen.com/i25012095/8446f28ac9738089.jpeg)
!!!、真机调试下,点击 Debug Memory Graph 才会显示FPS。搜索 MLeaksFinder 报的内存泄漏
![](https://img.haomeiwen.com/i23280551/f610a5f6d656f20c.png)
![](https://img.haomeiwen.com/i23280551/7dcbeba14ecd8789.png)