iOS打包出来的.ipa文件解压之后,得到Payload文件夹,右键显示包内容得到包里面的内容,但是无法看到可执行文件的组成内容及所占大小。
查看.ipa中的文件组成及所占大小。
1.生成LinkMap,更改配置。在Target的Build Settings中更改Write Link Map File 为 Yes,这样就可以生成Link Map文件了,但是这个文件在哪呢。通过修改Build Settings的Path To Link Map可以指定LinkMap文件的生成目录,默认是生成在Build文件夹下。
2.编译后得到LinkMap.txt文件。编译成功后在路径下得到/Users/xxxx/Library/Developer/Xcode/DerivedData/HelloWorld-fitgtiqfrissytdwtvkxxhkhpsfq/Build/Intermediates.noindex/HelloWorld.build/Debug-iphonesimulator/HelloWorld.build/HelloWorld-LinkMap-normal-x86_64.txt文件。
- LinkMap文件格式
# Path
# Arch: x86_64
# Object files:
# Sections:
# Symbols:
# Dead Stripped Symbols:
一个完整的LinkMap文件是分为这几块的,以#为分隔。
# Path: /Users/xxxx/Library/Developer/Xcode/DerivedData/HelloWorld-fitgtiqfrissytdwtvkxxhkhpsfq/Build/Products/Debug-iphonesimulator/HelloWorld.app/HelloWorld
Path记录的是这个LinkMap对应的安装包的地址。
# Arch: x86_64
Arch指的是这个LinkMap对应的架构。
# Object files:
[ 0] linker synthesized
[ 1] /Users/xxxx/Library/Developer/Xcode/DerivedData/HelloWorld-fitgtiqfrissytdwtvkxxhkhpsfq/Build/Intermediates.noindex/HelloWorld.build/Debug-iphonesimulator/HelloWorld.build/HelloWorld.app-Simulated.xcent
[ 2] /Users/xxxx/Library/Developer/Xcode/DerivedData/HelloWorld-fitgtiqfrissytdwtvkxxhkhpsfq/Build/Intermediates.noindex/HelloWorld.build/Debug-iphonesimulator/HelloWorld.build/Objects-normal/x86_64/ViewController.o
[ 3] /Users/xxxx/Library/Developer/Xcode/DerivedData/HelloWorld-fitgtiqfrissytdwtvkxxhkhpsfq/Build/Intermediates.noindex/HelloWorld.build/Debug-iphonesimulator/HelloWorld.build/Objects-normal/x86_64/main.o
[ 4] /Users/xxxx/Library/Developer/Xcode/DerivedData/HelloWorld-fitgtiqfrissytdwtvkxxhkhpsfq/Build/Intermediates.noindex/HelloWorld.build/Debug-iphonesimulator/HelloWorld.build/Objects-normal/x86_64/AppDelegate.o
[ 5] /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_iphonesimulator.a(arclite.o)
[ 6] /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator11.4.sdk/System/Library/Frameworks//Foundation.framework/Foundation.tbd
[ 7] /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator11.4.sdk/usr/lib/libobjc.tbd
[ 8] /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator11.4.sdk/usr/lib/libSystem.tbd
[ 9] /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator11.4.sdk/System/Library/Frameworks//CoreFoundation.framework/CoreFoundation.tbd
[ 10] /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator11.4.sdk/System/Library/Frameworks//UIKit.framework/UIKit.tbd
Object files是编译后生成的文件列表,比如这个程序class都编译成了.o文件,像我们比较熟悉的AppDelegate.o文件等等。还有引进来的几个库,比如UIKit.tbd。
# Sections:
# Address Size Segment Section
0x100001910 0x00001AD0 __TEXT __text
0x1000033E0 0x00000120 __TEXT __stubs
0x100003500 0x000001F0 __TEXT __stub_helper
0x1000036F0 0x00000BA2 __TEXT __objc_methname
0x100004292 0x00000065 __TEXT __objc_classname
0x1000042F7 0x0000087C __TEXT __objc_methtype
0x100004B73 0x0000024B __TEXT __cstring
0x100004DC0 0x00000008 __TEXT __const
0x100004DC8 0x00000178 __TEXT __entitlements
0x100004F40 0x000000B4 __TEXT __unwind_info
0x100005000 0x00000078 __DATA __got
0x100005078 0x00000010 __DATA __nl_symbol_ptr
0x100005088 0x00000180 __DATA __la_symbol_ptr
0x100005208 0x00000010 __DATA __objc_classlist
0x100005218 0x00000008 __DATA __objc_nlclslist
0x100005220 0x00000018 __DATA __objc_protolist
0x100005238 0x00000008 __DATA __objc_imageinfo
0x100005240 0x00000CF8 __DATA __objc_const
0x100005F38 0x000000B8 __DATA __objc_selrefs
0x100005FF0 0x00000008 __DATA __objc_protorefs
0x100005FF8 0x00000008 __DATA __objc_classrefs
0x100006000 0x00000008 __DATA __objc_superrefs
0x100006008 0x00000008 __DATA __objc_ivar
0x100006010 0x000000F0 __DATA __objc_data
0x100006100 0x00000160 __DATA __data
0x100006260 0x00000180 __DATA __bss
Section是各种数据类型所在的内存空间,Section主要分为两大类,__Text和__DATA。__Text指的是程序代码,__DATA指的是已经初始化的变量等。
具体分类如下表所示。
以下是__TEXT段的section
__text 主程序代码
stubs 和stub_helper 用于动态链接库的stub
__cstring c语言字符串
__const const修饰的常量
__objc_methname objc的方法名称
__objc_methtype objc方法类型
__objc_classname objc类方法
以下是__DATA段的section
__objc_ivars objc类的实例变量
__objc_classlist objc类列表
__objc_protolist objc协议列表
__objc_imageinfo objc镜像信息
__objc_const objc常量
__objc_selfrefs objc自引用(self)
__objc_protorefs objc协议引用
__objc_superrefs objc超类引用
__cfstring 使用Core Foundation字符串
__bss BSS
每个Section前面的两个16进制的数字代表的就是这个Section相对于安装包初始内存的偏移和这个Section的大小。比如:
0x100001910 0x00001AD0 __TEXT __text
__text这个Section的偏移地址是0x100001910,这块的大小是0x00001AD0,也就是6864个字节。
# Symbols:
# Address Size File Name
0x100001910 0x00000040 [ 2] -[ViewController viewDidLoad]
0x100001950 0x0000003C [ 2] -[ViewController didReceiveMemoryWarning]
0x100001990 0x00000090 [ 3] _main
0x100001A20 0x00000080 [ 4] -[AppDelegate application:didFinishLaunchingWithOptions:]
0x100001AA0 0x00000040 [ 4] -[AppDelegate applicationWillResignActive:]
0x100001AE0 0x00000040 [ 4] -[AppDelegate applicationDidEnterBackground:]
0x100001B20 0x00000040 [ 4] -[AppDelegate applicationWillEnterForeground:]
0x100001B60 0x00000040 [ 4] -[AppDelegate applicationDidBecomeActive:]
0x100001BA0 0x00000040 [ 4] -[AppDelegate applicationWillTerminate:]
0x100001BE0 0x00000020 [ 4] -[AppDelegate window]
0x100001C00 0x00000040 [ 4] -[AppDelegate setWindow:]
0x100001C40 0x00000033 [ 4] -[AppDelegate .cxx_destruct]
iOS开发的同学对Symbols这个单词肯定不陌生,什么Crash要有对应的符号表,编译的时候经常保持找不到Symbols等。Symbols简单来说就是类名,变量名,方法名等等符号。所以这一块也详细列出了这个安装包内各个方法所占的内存大小。
这一块太多了,我只列出一小部分,这块同样有四列,一二列和Sections的情况一样,分别是偏移地址和大小。第四列是方法的符号,类名+方法名。第三列是文件序号,这个序号是哪里来的的,就是前面提到的Object files里文件的序号,比如这里viewDidLoad的序号是2,去Object files去找序号是2的文件。
[ 2] /Users/xxxx/Library/Developer/Xcode/DerivedData/HelloWorld-fitgtiqfrissytdwtvkxxhkhpsfq/Build/Intermediates.noindex/HelloWorld.build/Debug-iphonesimulator/HelloWorld.build/Objects-normal/x86_64/ViewController.o
也就是说这个方法来自ViewController.o这个文件。
通过这种对应关系,我可以知道一个.o文件里有多少方法被编译进了安装包,每个方法所占的体积,加起来我就知道每个.o文件的大小了。后面的程序也就是把这个过程给自动化了。
# Dead Stripped Symbols:
# Size File Name
<<dead>> 0x00000018 [ 2] CIE
<<dead>> 0x00000018 [ 3] CIE
<<dead>> 0x00000006 [ 4] literal string: class
<<dead>> 0x00000008 [ 4] literal string: v16@0:8
<<dead>> 0x00000018 [ 4] CIE
<<dead>> 0x0000000F [ 5] literal string: isKindOfClass:
<<dead>> 0x00000005 [ 5] literal string: self
<<dead>> 0x00000008 [ 5] literal string: release
<<dead>> 0x0000000E [ 5] literal string: v32@0:8@16@24
<<dead>> 0x00000018 [ 5] CIE
最后还有一部分是没用的符号,这部分我也不知道是怎么产生的,但可以肯定的是这部分不应该太大。
3.自动分析:通过前面对LinkMap文件格式的解析,我们知道在LinkMap里,我们可以知道每个文件所占的体积大小,并且可以通过文件的前缀,知道文件所属的动态库,这样也就可以知道动态库的大小。只是这个过程太过繁琐,所以我们去把它自动化了。项目开源地址:LinkMapParser
- Mac自带python环境。Python环境安装
➜ LinkMapParser-master python --version
Python 2.7.10
- 该工具支持分析一个link map文件和比较两个link map文件,进入下载的开源项目LinkMapParser目录,运行的命令分别为:
python parselinkmap.py /Users/xxxx/Library/Developer/Xcode/DerivedData/HelloWorld-fitgtiqfrissytdwtvkxxhkhpsfq/Build/Intermediates.noindex/HelloWorld.build/Debug-iphonesimulator/HelloWorld.build/HelloWorld-LinkMap-normal-x86_64.txt
python parselinkmap.py /Users/xxxx/Library/Developer/Xcode/DerivedData/HelloWorld-fitgtiqfrissytdwtvkxxhkhpsfq/Build/Intermediates.noindex/HelloWorld.build/Debug-iphonesimulator/HelloWorld.build/HelloWorld-LinkMap-normal-x86_64.txt /Users/xxxx/Library/Developer/Xcode/DerivedData/AddChildViewController-fmbyhwgpcjsxuzgomqidebjtmwnx/Build/Intermediates.noindex/AddChildViewController.build/Debug-iphonesimulator/AddChildViewController.build/AddChildViewController-LinkMap-normal-x86_64.txt