ios 进阶

【iOS内存调试】Debug Memory Graph

2021-05-14  本文已影响0人  文子飞_

1、作用

可在程序运行时,排查和定位内存泄漏问题。

2、打开Malloc stack logging

打开此设置可以backtrace对象堆栈信息。


image.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

image image

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,点击屏幕开启定时器。

image image

!!!、真机调试下,点击 Debug Memory Graph 才会显示FPS。搜索 MLeaksFinder 报的内存泄漏

image.png image.png
上一篇 下一篇

猜你喜欢

热点阅读