基础分析
git clone --recursive https://github.com/google/bloaty
cd bloaty
git submodule update
mkdir build
cd build
cmake ..
make -j8
bloaty bloaty
FILE SIZE VM SIZE
-------------- --------------
35.5% 16.9Mi 0.0% 0 .debug_info
25.2% 12.0Mi 0.0% 0 .debug_loc
11.2% 5.36Mi 0.0% 0 .debug_str
6.9% 3.29Mi 0.0% 0 .debug_ranges
5.8% 2.75Mi 0.0% 0 .debug_line
5.2% 2.46Mi 34.0% 2.46Mi .rodata
4.6% 2.21Mi 30.6% 2.21Mi .text
0.0% 0 14.0% 1.01Mi .bss
1.1% 556Ki 0.0% 0 .strtab
0.9% 441Ki 6.0% 441Ki .data
0.8% 395Ki 5.4% 395Ki .rela.dyn
0.7% 344Ki 4.7% 344Ki .data.rel.ro
0.7% 334Ki 0.0% 0 .debug_abbrev
0.6% 274Ki 3.7% 274Ki .eh_frame
0.5% 226Ki 0.0% 0 .symtab
0.1% 50.5Ki 0.7% 50.5Ki .gcc_except_table
0.1% 49.3Ki 0.7% 49.3Ki .eh_frame_hdr
0.1% 37.0Ki 0.0% 0 .debug_aranges
0.0% 21.6Ki 0.3% 18.7Ki [25 Others]
0.0% 6.67Ki 0.0% 0 [Unmapped]
0.0% 6.06Ki 0.1% 6.06Ki .dynstr
100.0% 47.7Mi 100.0% 7.22Mi TOTA
源文件分析
bloaty bloaty -d compileunits
FILE SIZE VM SIZE
-------------- --------------
37.0% 17.6Mi 48.6% 3.51Mi [155 Others]
15.1% 7.22Mi 3.5% 258Ki /home/vboxuser/Desktop/bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc
6.5% 3.09Mi 2.9% 216Ki /home/vboxuser/Desktop/bloaty/third_party/protobuf/src/google/protobuf/descriptor.pb.cc
4.8% 2.28Mi 1.4% 100Ki /home/vboxuser/Desktop/bloaty/third_party/protobuf/src/google/protobuf/text_format.cc
4.7% 2.25Mi 5.7% 424Ki /home/vboxuser/Desktop/bloaty/third_party/capstone/arch/ARM/ARMDisassembler.c
3.1% 1.48Mi 1.2% 90.7Ki /home/vboxuser/Desktop/bloaty/third_party/protobuf/src/google/protobuf/generated_message_reflection.cc
2.6% 1.25Mi 0.5% 33.3Ki /home/vboxuser/Desktop/bloaty/third_party/protobuf/src/google/protobuf/descriptor_database.cc
2.4% 1.15Mi 0.9% 68.6Ki /home/vboxuser/Desktop/bloaty/third_party/protobuf/src/google/protobuf/extension_set.cc
2.3% 1.07Mi 0.4% 31.6Ki /home/vboxuser/Desktop/bloaty/third_party/protobuf/src/google/protobuf/generated_message_util.cc
0.8% 379Ki 14.6% 1.06Mi /home/vboxuser/Desktop/bloaty/third_party/capstone/arch/M68K/M68KDisassembler.c
2.1% 1.02Mi 0.8% 60.9Ki /home/vboxuser/Desktop/bloaty/third_party/protobuf/src/google/protobuf/wire_format.cc
2.1% 1.02Mi 1.5% 111Ki /home/vboxuser/Desktop/bloaty/third_party/capstone/arch/AArch64/AArch64Disassembler.c
2.0% 981Ki 11.7% 862Ki /home/vboxuser/Desktop/bloaty/third_party/capstone/arch/X86/X86Mapping.c
2.0% 981Ki 1.1% 79.1Ki /home/vboxuser/Desktop/bloaty/third_party/capstone/arch/SystemZ/SystemZDisassembler.c
2.0% 970Ki 0.5% 37.1Ki /home/vboxuser/Desktop/bloaty/third_party/re2/re2/re2.cc
1.9% 920Ki 0.8% 60.3Ki /home/vboxuser/Desktop/bloaty/third_party/protobuf/src/google/protobuf/map_field.cc
1.9% 920Ki 0.5% 33.3Ki /home/vboxuser/Desktop/bloaty/third_party/re2/re2/dfa.cc
1.8% 889Ki 1.7% 125Ki /home/vboxuser/Desktop/bloaty/src/bloaty.cc
1.6% 788Ki 0.5% 33.7Ki /home/vboxuser/Desktop/bloaty/third_party/re2/re2/regexp.cc
1.6% 781Ki 0.7% 51.4Ki /home/vboxuser/Desktop/bloaty/third_party/protobuf/src/google/protobuf/repeated_field.cc
1.6% 766Ki 0.5% 37.9Ki /home/vboxuser/Desktop/bloaty/third_party/protobuf/src/google/protobuf/message.cc
100.0% 47.7Mi 100.0% 7.22Mi TOTAL
测试
#include <iostream>
const char* data = "而此时是ii四十九而此时是ii四十九而此时是ii四十九而此时是ii四十九而此时是ii四十九而此时是ii四十九而此时是ii四十九而此时是ii四十九而此时是ii四十九而此时是ii四十九";
int main() {
printf("test main\n");
}
g++ -g main.cpp -o mytest
bloaty mytest -d compileunits
FILE SIZE VM SIZE
-------------- --------------
56.9% 21.6Ki 16.6% 609 main.cpp
22.2% 8.46Ki 0.0% 0 [Unmapped]
5.9% 2.25Ki 0.0% 0 [ELF Section Headers]
4.1% 1.57Ki 0.0% 0 [section .symtab]
1.9% 728 19.9% 728 [ELF Program Headers]
1.5% 603 0.0% 0 [section .strtab]
1.3% 512 14.0% 512 [section .dynamic]
1.0% 394 9.2% 335 [16 Others]
0.9% 346 0.0% 0 [section .shstrtab]
0.6% 247 6.7% 247 [section .rodata]
0.6% 240 6.6% 240 [section .dynsym]
0.6% 233 6.4% 233 [section .text]
0.6% 229 6.3% 229 [section .dynstr]
0.4% 152 0.0% 0
0.3% 136 3.7% 136 [section .eh_frame]
0.2% 88 2.4% 88 [section .got]
0.2% 80 2.2% 80 [section .gnu.version_r]
0.2% 64 1.7% 64 [ELF Header]
0.2% 64 1.7% 64 [section .plt]
0.1% 48 1.3% 48 [section .note.gnu.property]
0.1% 48 1.3% 48 [section .plt.sec]
100.0% 38.0Ki 100.0% 3.58Ki TOTAL
参考
C++ exe程序内存占用分析-CSDN博客
创作不易,小小的支持一下吧!
![](https://img-blog.csdnimg.cn/direct/2d50062d779a47afb478b75948ec28e5.png)
![](https://img-blog.csdnimg.cn/direct/d4a3f7989b534e9b8b67a6fa085b1129.jpeg)