FBMemoryProfiler的使用

作者: cuagain | 来源:发表于2017-03-26 16:49 被阅读632次

FBmemoryProfiler是FB开源的一款用于分析iOS内存使用和检测循环引用的工具库。
上一篇我们对FBMemoryProfiler进行了一通分析(其实是FB讲解的),那么我们怎么使用呢,接下来简单介绍一下。

安装
CocoaPods

我们只要在pod文件中添加这么一句就👌🏼了:

pod 'FBMemoryProfiler', '~> 0.1.3'

FBMemoryProfiler最低支持iOS8.0,如果你的pod文件上最低要求是6或者7的话,是无法直接安装的。所以,建议在开发分支上或者如果有多个target的话,在开发的target上添加FBMemoryProfiler。在生产的target上不需要添加FBMemoryProfiler。
配置好后执行 <code>pod install</code>,安装好后,打开对应的.xcworkspace文件就能看到对应的库。

Carthage

Carthage是迥异于CocoaPods的轻量级、非侵入式的包管理工具。FBMemoryProfiler同样支持使用Carthage来安装。
在创建的Cartfile文件中添加:

github "facebook/FBMemoryProfiler"

之后运行Carthage update --configuration Debug即可。

嵌入代码

首先,在main.m文件中添加FBRetainCycleDetector的hook,同时也要开启FBAllocationTracker的生成追踪:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#if DEBUG
#import     <FBAllocationTracker/FBAllocationTracker.h>
#import <FBRetainCycleDetector/FBRetainCycleDetector.h>
#endif
int main(int argc, char * argv[]) {
#if DEBUG
    [FBAssociationManager hook];
    [[FBAllocationTrackerManager sharedManager] startTrackingAllocations];
    [[FBAllocationTrackerManager sharedManager] enableGenerations];
#endif
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

之后,我们要在AppDelegate.m的-application:didFinishLaunchingWithOptions:方法中嵌入FBMemoryProfiler的创建代码:

#import "AppDelegate.h"
#if DEBUG
#import <FBMemoryProfiler/FBMemoryProfiler.h>
#import <FBRetainCycleDetector/FBRetainCycleDetector.h>
#import "CacheCleanerPlugin.h"
#import "RetainCycleLoggerPlugin.h"
#endif
@interface AppDelegate ()
{
    FBMemoryProfiler *memoryProfiler;
}
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#if DEBUG
    memoryProfiler = [[FBMemoryProfiler alloc]initWithPlugins:@[[CacheCleanerPlugin new],[RetainCycleLoggerPlugin new]] retainCycleDetectorConfiguration:nil];
    [memoryProfiler enable];
#endif
    // Override point for customization after application launch.
    return YES;
}

其中,插件是可以不开启的,要开启的话需要创建两个文件,文件在这里Plugins章节里可以看到。如果不开启的话,实例化的方法如下:

memoryProfiler = [FBMemoryProfiler new];
[memoryProfiler enable];

插件主要是用来进行过滤、去重或者输出、存储等操作的,毕竟,如果不开启插件的话,只能通过手机、模拟器上点击屏幕来看内存泄漏,而如果自定义log插件的话,可以将捕获到的内存泄漏输出到控制台或者文件中。
比如,我们可以自定义一个RetainCycleLoggerPlugin,使用FBMemoryProfilerPluggable协议,重写memoryProfilerDidFindRetainCycles:方法:

- (void)memoryProfilerDidFindRetainCycles:(NSSet *)retainCycles
{
    if (retainCycles.count > 0) {
        NSLog(@"\nretainCycles = \n%@",retainCycles);
    }
}

当FBRetainCycleDetector找到循环引用之后,就会调用到上面的方法。
但是呢,在测试时候会返现,会输出很多无用信息,类似下边这种:

retainCycles = 
{(
        (
        "-> _testObject -> TestObject ",
        "-> _viewController -> ViewController "
    ),
        (
        "-> target -> FBMemoryProfilerViewController ",
        "-> _timer -> __NSCFTimer "
    ),
        (
        "-> UIPanGestureRecognizer ",
        "-> _internalActiveTouches -> __NSSetM ",
        "-> (null) ",
        "-> _gestureRecognizers -> (null) "
    )
)}

这样的信息,跟写的代码毫无关联,可能是系统内部的一些潜在的输出,应该类似一些临时cache之类的机制,并不会是真正的内存泄漏,对于这样的情况,我们可以将其过滤一下。过滤的代码类似于:

NSArray *filters = @[FBFilterBlockWithObjectIvarRelation([UIView class], @"_subviewCache")];
FBObjectGraphConfiguration *configuratin = [[FBObjectGraphConfiguration alloc]initWithFilterBlocks:filters shouldInspectTimers:YES];
memoryProfiler = [[FBMemoryProfiler alloc]initWithPlugins:@[[CacheCleanerPlugin new],[RetainCycleLoggerPlugin new]] retainCycleDetectorConfiguration:configuratin];
[memoryProfiler enable];

设置一个过滤数组,然后添加到FBMemoryProfiler的configuration中即可。
对于确信没有问题或者不想修改的问题,可以直接过滤。
比如:
NSArray *filters = @[FBFilterBlockWithObjectIvarRelation([UIPanGestureRecognizer class], @"_internalActiveTouches")]

运行效果
示例

综上,要集成到项目里并灵活运用,还需要更多的尝试,但这不失为一种提高工作效率的方式。

相关文章

网友评论

    本文标题:FBMemoryProfiler的使用

    本文链接:https://www.haomeiwen.com/subject/ilekottx.html