FBMemoryProfiler 框架详细解析(一)

版本记录

版本号 时间
V1.0 2017.09.26

前言

在APP中,内存是极其宝贵的资源,因此需要我们很好的管理内存的使用,下面介绍一个用于内存管理的框架,这里先给出Github - FBMemoryProfiler

关于

iOS库提供开发人员工具,用于随时间浏览内存中的对象,使用FBAllocationTrackerFBRetainCycleDetector

它使用FBAllocationTracker收集有关对象的信息。 它支持generations 和引用循环检测retain cycle detection

下面就是一个小的demo,例子中有 available in Example directory


安装

1. Carthage

在Cartfile文件中加入

github "facebook/FBMemoryProfiler"

接着

carthage update --configuration Debug

2. CocoaPods

pod文件中加入

pod 'FBMemoryProfiler'

您只能在调试版本中完全使用FBMemoryProfiler。 这由编译标志控制,可以提供给构建以使其在其他配置中工作。


使用

要开始使用FBMemoryProfiler,您首先需要启用FBAllocationTracker

#import <FBAllocationTracker/FBAllocationTrackerManager.h>

int main(int argc, char * argv[]) {
  [[FBAllocationTrackerManager sharedManager] startTrackingAllocations];
  [[FBAllocationTrackerManager sharedManager] enableGenerations];
  @autoreleasepool {
      return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
  }
}

启用memory profiler

#import <FBMemoryProfiler/FBMemoryProfiler.h>

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

// Store memory profiler somewhere to extend it's lifetime
_memoryProfiler = memoryProfiler;

FBMemoryProfiler将显示为屏幕上的按钮。 一旦点击,它将以全尺寸模式打开内存分析器。我们还可以定义插件(检查下面)和引用循环检测器的过滤器,我们传递给配置。

_memoryProfiler = [[FBMemoryProfiler alloc] initWithPlugins:@[[IncredibleCacheCleaningPlugin new],
                                                              [AwesomeLoggerPlugin new]]
                           retainCycleDetectorConfiguration:someConfigurationWithCustomFilters];
[_memoryProfiler enable];

后记

未完,待续~~~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容