MLeaksFinder这个库可以监测内存泄露。不需要任何代码。导入就行了。控制台会打印,屏幕上还会弹出来哪个对象没有释放(debug模式下)。
882519B6-E966-4226-A533-F0C0639497FD.png
9281AACE-522A-4327-B23B-187D0D897143.png
等发布的时候改为release,不会出现打印的结果屏幕也不会弹出AleterController。
ps:有时候会误报内存泄露。在静态cell上放了一个UISwithch,pop后会误报开关没有被销毁。可以添加白名单。在NSObject+MemoryLeak分类中
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #ffffff}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #4cbf57}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #e44448}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #00afca}span.s3 {font-variant-ligatures: no-common-ligatures; color: #c2349b}span.s4 {font-variant-ligatures: no-common-ligatures; color: #d28f5a}span.s5 {font-variant-ligatures: no-common-ligatures; color: #ffffff}span.s6 {font-variant-ligatures: no-common-ligatures; color: #e44448}
+ (NSSet *)classNamesInWhiteList {
static NSSet *whiteList;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
whiteList = [NSSet setWithObjects:
@"UIFieldEditor", // UIAlertControllerTextField
@"UINavigationBar",
@"_UIAlertControllerActionView",
@"_UIVisualEffectBackdropView",
@"UISwitch", //添加UISwitch为白名单
nil];
});
return whiteList;
}