之前APP占用太大内存,查找i内存泄露、内存消耗过大
-
查找泄漏
在xcode自带两种内存泄漏查询工具
1> Analyze: 静态分析工具 - 查 找: 可以通过 Product ->Analyze 菜单项启动- 快捷键: CMD+shift +b
Analyze主要分析以下四种问题:
根据这个图标查看内存泄漏疑点.png
- 逻辑错误:访问空指针或未初始化的变量等;
- 内存管理错误:如内存泄漏等;
- 声明错误:从未使用过的变量;
- Api调用错误:未包含使用的库和框架。
1、Value stored to 'xxx' during its initialization is never read // 对象声明之后根本就没有使用 只有赋值
2、Value stored to 'xxx' is never read // 对象声明之后根本就没有使用 只有赋值
3、function call argument is an uninitialized value // 所调用的方法没有初始化值
4、The 'viewWillAppear:' instance method in UIViewController subclass 'xxx' is missing a [super viewWillAppear:] call // 调用此类方法时需实现父类方法
5、Argument to 'NSMutableArray' method 'addObject:' cannot be nil // 被添加的对象不能为nil
这个工具会把错误进行分类,每个分类下可以查看到具体的代码行。

常见问题举例
1)Localizability Issue (Apple)

意思是:面向用户的文本应使用本地化的字符串宏。
我的项目中错误最多的就是这个,因为很多地方的字符串没有做本地化处理或者宏定义。
解决办法:
1、在项目中添加本地化。
2、或者在 Build Settings 里面搜索“localizability”找到 Missing Localizability(缺少本地化) 设置为 NO,就会忽略这个问题。如下图:

2)Core Foundation/Objective-C


意思是:我这里漏写了 [super viewDidLoad];
解决办法: 补上 [super viewDidLoad];
3)Logic error


意思是:这个对象从来没有被读取使用过。
解决办法: 删掉永远不会被使用的对象声明,或者查找问题是不是该使用但是没有使用。
4)Memory error


“nil returned from a method that is expected to return a non-null value”
意思是:不能传空却有可能传了空值。
解决办法: 我这里是因为只写了else if 的判断却没有 else 的判断,所以有一定几率返回的cell是空的,把else if 改成 else 就好了。其他情景也类似,只要不该空的时候不传空就行。
5)Memory (Core Foundation/Objective-C)


“Potential leak of an object stored into 'nextBuffer'”
意思是:nextBuffer这个对象存在内存泄漏。
解决办法: 检查是否存在内存泄漏的可能。
6)Dead store


“Value stored to 'replyString' during its initialization is never read”
意思是:这个对象从来没有被读取使用过。
解决办法: 删掉永远不会被使用的对象声明,或者查找问题是不是该使用但是没有使用。
2 >Instruments: 动态分析工具- 查 找: Product ->Profile 菜单项启动- 快捷键: CMD + i.- 简 介:它有很多跟踪模块可以动态分析和跟踪内存, CPU 和文件系统.

界面的介绍
在 instruments 中,虽然选择了 Leaks 模板,但默认情况下也会添加 Allocations 模板.基本上凡是内存分析都会使用 Allocations 模板, 它可以监控内存分布情况。
① 选中 Allocations 模板,(图1区域),右边的3区域会显示随着时间的变化内存使用的折线图,同时在4区域会显示内存使用的详细信息,以及对象分配情况.
② 点击 Leaks 模板(图中2区域), 可以查看内存泄露情况。如果在3区域有 红X 出现, 则有内存泄露, 4区域则会显示泄露的对象.
- 打用leaks进行监测:
点击泄露对象可以在(下图)看到它们的内存地址, 占用字节, 所属框架和响应方法等信息.打开扩展视图, 可以看到右边的跟踪堆栈信息

leaks进行监测
-
监测结果的分析:
勾选Call Tree可以将其他的信息过滤直接看到我们代码中内存泄露的部分.png
这是关于查询内存泄露的部分的操作。至于怎么查看内存消耗过多的部分其实是跟leaks的使用是类似的。
同样在instrument面板上选中Allocations

具体的操作跟leaks的操作类似,大致的可以得出具体是哪个地方内存消耗过大,其次还有xcode8新出的debugMemoryGraph对内存调试也很有帮助,再这里就不一一描述了。
网友评论