首页 > 编程语言 > 详细

C++和C程序内存占用对比分析

时间:2020-08-09 23:37:20      阅读:178      评论:0      收藏:0      [点我收藏+]

C++和C程序内存占用对比分析

本例以简单程序为例,分析C++和C语言的内存占用情况。本运行环境为:

  • Ubuntu 20.04 LTS
  • G++/GCC version: 9.3.0
  • libstdc++.so.6.0.28/libc-2.31.so

 

1,C代码文件为hello.c,C++代码文件为hello.cpp:

/* hello.c */
#include <stdio.h>
#include <unistd.h>

int main(void)
{
        printf("hello world\n");
        pause();
        return 0x0;
}
/* hello.cpp */
#include <iostream>
#include <cstdlib>
#include <unistd.h>

int main(void)
{
        std::cout << "hello world" << std::endl;
        pause();
        return 0x0;
}

 

2,编译

gcc hello.c -o c.out
g++ hello.cpp -o cpp.out

3,可执行文件大小

从下面的表格中可以看到:

  • C++编译之后的可执行文件,符号表的长度比C语言更多一些,strip之后可执行文件的size(占用的磁盘大小)是相同的;
  • C++代码段比C语言大;
  • Data相差不大(此例没有用到全局变量和静态变量),C++还是比C略大一些;
  • BSS段相差比较大,说明C++有很多的全局变量/静态变量;

 

File Name Size Size(AfterStrip) Text Data BSS
c.out 16728 14464 1653 608 8
cpp.out 17352 14464 2543 672 280
Delta(C++ - C) 624 0 890 64 272

 

 

 

 

 

 

1 xinpengc@xinpengc-VirtualBox:~/learn/cpp$ stat c.out cpp.out
 2   File: c.out
 3   Size: 16728           Blocks: 40         IO Block: 4096   regular file
 4 Device: 805h/2053d      Inode: 928442      Links: 1
 5 Access: (0775/-rwxrwxr-x)  Uid: ( 1000/xinpengc)   Gid: ( 1000/xinpengc)
 6 Access: 2020-08-09 22:02:43.181218973 +0800
 7 Modify: 2020-08-09 22:02:43.181218973 +0800
 8 Change: 2020-08-09 22:02:43.181218973 +0800
 9  Birth: -
10   File: cpp.out
11   Size: 17352           Blocks: 40         IO Block: 4096   regular file
12 Device: 805h/2053d      Inode: 928445      Links: 1
13 Access: (0775/-rwxrwxr-x)  Uid: ( 1000/xinpengc)   Gid: ( 1000/xinpengc)
14 Access: 2020-08-09 22:02:49.201218761 +0800
15 Modify: 2020-08-09 22:02:49.201218761 +0800
16 Change: 2020-08-09 22:02:49.201218761 +0800
17  Birth: -
18 xinpengc@xinpengc-VirtualBox:~/learn/cpp$ size c.out cpp.out
19    text    data     bss     dec     hex filename
20    1653     608       8    2269     8dd c.out
21    2543     672     280    3495     da7 cpp.out
22 xinpengc@xinpengc-VirtualBox:~/learn/cpp$
23 xinpengc@xinpengc-VirtualBox:~/learn/cpp$ strip c.out cpp.out
24 xinpengc@xinpengc-VirtualBox:~/learn/cpp$ stat c.out cpp.out
25   File: c.out
26   Size: 14464           Blocks: 32         IO Block: 4096   regular file
27 Device: 805h/2053d      Inode: 928447      Links: 1
28 Access: (0775/-rwxrwxr-x)  Uid: ( 1000/xinpengc)   Gid: ( 1000/xinpengc)
29 Access: 2020-08-09 22:03:36.333217100 +0800
30 Modify: 2020-08-09 22:03:36.333217100 +0800
31 Change: 2020-08-09 22:03:36.333217100 +0800
32  Birth: -
33   File: cpp.out
34   Size: 14464           Blocks: 32         IO Block: 4096   regular file
35 Device: 805h/2053d      Inode: 928442      Links: 1
36 Access: (0775/-rwxrwxr-x)  Uid: ( 1000/xinpengc)   Gid: ( 1000/xinpengc)
37 Access: 2020-08-09 22:03:36.333217100 +0800
38 Modify: 2020-08-09 22:03:36.333217100 +0800
39 Change: 2020-08-09 22:03:36.333217100 +0800
40  Birth: -
41 xinpengc@xinpengc-VirtualBox:~/learn/cpp$
42 xinpengc@xinpengc-VirtualBox:~/learn/cpp$ size c.out cpp.out
43    text    data     bss     dec     hex filename
44    1653     608       8    2269     8dd c.out
45    2543     672     280    3495     da7 cpp.out
46 xinpengc@xinpengc-VirtualBox:~/learn/cpp$
47 xinpengc@xinpengc-VirtualBox:~/learn/cpp$

 

 

4,运行时分析

从maps分析,hello.cpp使用了pause()导致链接了C标准库,所以程序占用的内存大小可比性不够好,下一个版本再进行详细分析。

FileName VmHWM(KB) VmRSS(KB)
c.out(hello.c) 580 580
cpp.out(hello.cpp) 1528 1528
Delta 948 948

 

 

 

 

 

 

1 xinpengc@xinpengc-VirtualBox:~/learn/cpp$ ./c.out &
  2 [1] 3340
  3 xinpengc@xinpengc-VirtualBox:~/learn/cpp$ hello world
  4 ./cpp.out &
  5 [2] 3341
  6 xinpengc@xinpengc-VirtualBox:~/learn/cpp$ hello world
  7 xinpengc@xinpengc-VirtualBox:~/learn/cpp$ cat /proc/3340/status /proc/3340/maps
  8 Name:   c.out
  9 Umask:  0002
 10 State:  S (sleeping)
 11 Tgid:   3340
 12 Ngid:   0
 13 Pid:    3340
 14 PPid:   2112
 15 TracerPid:      0
 16 Uid:    1000    1000    1000    1000
 17 Gid:    1000    1000    1000    1000
 18 FDSize: 256
 19 Groups: 4 24 27 30 46 120 131 132 1000
 20 NStgid: 3340
 21 NSpid:  3340
 22 NSpgid: 3340
 23 NSsid:  2112
 24 VmPeak:     2488 kB
 25 VmSize:     2488 kB
 26 VmLck:         0 kB
 27 VmPin:         0 kB
 28 VmHWM:       580 kB
 29 VmRSS:       580 kB
 30 RssAnon:              64 kB
 31 RssFile:             516 kB
 32 RssShmem:              0 kB
 33 VmData:      180 kB
 34 VmStk:       132 kB
 35 VmExe:         8 kB
 36 VmLib:      1644 kB
 37 VmPTE:        40 kB
 38 VmSwap:        0 kB
 39 HugetlbPages:          0 kB
 40 CoreDumping:    0
 41 THP_enabled:    1
 42 Threads:        1
 43 SigQ:   0/7731
 44 SigPnd: 0000000000000000
 45 ShdPnd: 0000000000000000
 46 SigBlk: 0000000000000000
 47 SigIgn: 0000000000000000
 48 SigCgt: 0000000000000000
 49 CapInh: 0000000000000000
 50 CapPrm: 0000000000000000
 51 CapEff: 0000000000000000
 52 CapBnd: 0000003fffffffff
 53 CapAmb: 0000000000000000
 54 NoNewPrivs:     0
 55 Seccomp:        0
 56 Speculation_Store_Bypass:       vulnerable
 57 Cpus_allowed:   1
 58 Cpus_allowed_list:      0
 59 Mems_allowed:   00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
 60 Mems_allowed_list:      0
 61 voluntary_ctxt_switches:        1
 62 nonvoluntary_ctxt_switches:     0
 63 55d72012f000-55d720130000 r--p 00000000 08:05 928447                     /home/xinpengc/learn/cpp/c.out
 64 55d720130000-55d720131000 r-xp 00001000 08:05 928447                     /home/xinpengc/learn/cpp/c.out
 65 55d720131000-55d720132000 r--p 00002000 08:05 928447                     /home/xinpengc/learn/cpp/c.out
 66 55d720132000-55d720133000 r--p 00002000 08:05 928447                     /home/xinpengc/learn/cpp/c.out
 67 55d720133000-55d720134000 rw-p 00003000 08:05 928447                     /home/xinpengc/learn/cpp/c.out
 68 55d72027e000-55d72029f000 rw-p 00000000 00:00 0                          [heap]
 69 7fb897b56000-7fb897b7b000 r--p 00000000 08:05 1840863                    /usr/lib/x86_64-linux-gnu/libc-2.31.so
 70 7fb897b7b000-7fb897cf3000 r-xp 00025000 08:05 1840863                    /usr/lib/x86_64-linux-gnu/libc-2.31.so
 71 7fb897cf3000-7fb897d3d000 r--p 0019d000 08:05 1840863                    /usr/lib/x86_64-linux-gnu/libc-2.31.so
 72 7fb897d3d000-7fb897d3e000 ---p 001e7000 08:05 1840863                    /usr/lib/x86_64-linux-gnu/libc-2.31.so
 73 7fb897d3e000-7fb897d41000 r--p 001e7000 08:05 1840863                    /usr/lib/x86_64-linux-gnu/libc-2.31.so
 74 7fb897d41000-7fb897d44000 rw-p 001ea000 08:05 1840863                    /usr/lib/x86_64-linux-gnu/libc-2.31.so
 75 7fb897d44000-7fb897d4a000 rw-p 00000000 00:00 0
 76 7fb897d5c000-7fb897d5d000 r--p 00000000 08:05 1840650                    /usr/lib/x86_64-linux-gnu/ld-2.31.so
 77 7fb897d5d000-7fb897d80000 r-xp 00001000 08:05 1840650                    /usr/lib/x86_64-linux-gnu/ld-2.31.so
 78 7fb897d80000-7fb897d88000 r--p 00024000 08:05 1840650                    /usr/lib/x86_64-linux-gnu/ld-2.31.so
 79 7fb897d89000-7fb897d8a000 r--p 0002c000 08:05 1840650                    /usr/lib/x86_64-linux-gnu/ld-2.31.so
 80 7fb897d8a000-7fb897d8b000 rw-p 0002d000 08:05 1840650                    /usr/lib/x86_64-linux-gnu/ld-2.31.so
 81 7fb897d8b000-7fb897d8c000 rw-p 00000000 00:00 0
 82 7fffeb55d000-7fffeb57e000 rw-p 00000000 00:00 0                          [stack]
 83 7fffeb5ee000-7fffeb5f1000 r--p 00000000 00:00 0                          [vvar]
 84 7fffeb5f1000-7fffeb5f2000 r-xp 00000000 00:00 0                          [vdso]
 85 ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0                  [vsyscall]
 86 xinpengc@xinpengc-VirtualBox:~/learn/cpp$
 87 xinpengc@xinpengc-VirtualBox:~/learn/cpp$
 88 xinpengc@xinpengc-VirtualBox:~/learn/cpp$ cat /proc/3341/status /proc/3341/maps
 89 Name:   cpp.out
 90 Umask:  0002
 91 State:  S (sleeping)
 92 Tgid:   3341
 93 Ngid:   0
 94 Pid:    3341
 95 PPid:   2112
 96 TracerPid:      0
 97 Uid:    1000    1000    1000    1000
 98 Gid:    1000    1000    1000    1000
 99 FDSize: 256
100 Groups: 4 24 27 30 46 120 131 132 1000
101 NStgid: 3341
102 NSpid:  3341
103 NSpgid: 3341
104 NSsid:  2112
105 VmPeak:     5876 kB
106 VmSize:     5876 kB
107 VmLck:         0 kB
108 VmPin:         0 kB
109 VmHWM:      1528 kB
110 VmRSS:      1528 kB
111 RssAnon:             156 kB
112 RssFile:            1372 kB
113 RssShmem:              0 kB
114 VmData:      228 kB
115 VmStk:       132 kB
116 VmExe:         8 kB
117 VmLib:      3344 kB
118 VmPTE:        44 kB
119 VmSwap:        0 kB
120 HugetlbPages:          0 kB
121 CoreDumping:    0
122 THP_enabled:    1
123 Threads:        1
124 SigQ:   0/7731
125 SigPnd: 0000000000000000
126 ShdPnd: 0000000000000000
127 SigBlk: 0000000000000000
128 SigIgn: 0000000000000000
129 SigCgt: 0000000000000000
130 CapInh: 0000000000000000
131 CapPrm: 0000000000000000
132 CapEff: 0000000000000000
133 CapBnd: 0000003fffffffff
134 CapAmb: 0000000000000000
135 NoNewPrivs:     0
136 Seccomp:        0
137 Speculation_Store_Bypass:       vulnerable
138 Cpus_allowed:   1
139 Cpus_allowed_list:      0
140 Mems_allowed:   00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
141 Mems_allowed_list:      0
142 voluntary_ctxt_switches:        1
143 nonvoluntary_ctxt_switches:     0
144 5640b9d4d000-5640b9d4e000 r--p 00000000 08:05 928442                     /home/xinpengc/learn/cpp/cpp.out
145 5640b9d4e000-5640b9d4f000 r-xp 00001000 08:05 928442                     /home/xinpengc/learn/cpp/cpp.out
146 5640b9d4f000-5640b9d50000 r--p 00002000 08:05 928442                     /home/xinpengc/learn/cpp/cpp.out
147 5640b9d50000-5640b9d51000 r--p 00002000 08:05 928442                     /home/xinpengc/learn/cpp/cpp.out
148 5640b9d51000-5640b9d52000 rw-p 00003000 08:05 928442                     /home/xinpengc/learn/cpp/cpp.out
149 5640bb6df000-5640bb700000 rw-p 00000000 00:00 0                          [heap]
150 7f8c07e56000-7f8c07e5a000 rw-p 00000000 00:00 0
151 7f8c07e5a000-7f8c07e5d000 r--p 00000000 08:05 1841148                    /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
152 7f8c07e5d000-7f8c07e6f000 r-xp 00003000 08:05 1841148                    /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
153 7f8c07e6f000-7f8c07e73000 r--p 00015000 08:05 1841148                    /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
154 7f8c07e73000-7f8c07e74000 r--p 00018000 08:05 1841148                    /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
155 7f8c07e74000-7f8c07e75000 rw-p 00019000 08:05 1841148                    /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
156 7f8c07e75000-7f8c07e84000 r--p 00000000 08:05 1841501                    /usr/lib/x86_64-linux-gnu/libm-2.31.so
157 7f8c07e84000-7f8c07f2b000 r-xp 0000f000 08:05 1841501                    /usr/lib/x86_64-linux-gnu/libm-2.31.so
158 7f8c07f2b000-7f8c07fc2000 r--p 000b6000 08:05 1841501                    /usr/lib/x86_64-linux-gnu/libm-2.31.so
159 7f8c07fc2000-7f8c07fc3000 r--p 0014c000 08:05 1841501                    /usr/lib/x86_64-linux-gnu/libm-2.31.so
160 7f8c07fc3000-7f8c07fc4000 rw-p 0014d000 08:05 1841501                    /usr/lib/x86_64-linux-gnu/libm-2.31.so
161 7f8c07fc4000-7f8c07fe9000 r--p 00000000 08:05 1840863                    /usr/lib/x86_64-linux-gnu/libc-2.31.so
162 7f8c07fe9000-7f8c08161000 r-xp 00025000 08:05 1840863                    /usr/lib/x86_64-linux-gnu/libc-2.31.so
163 7f8c08161000-7f8c081ab000 r--p 0019d000 08:05 1840863                    /usr/lib/x86_64-linux-gnu/libc-2.31.so
164 7f8c081ab000-7f8c081ac000 ---p 001e7000 08:05 1840863                    /usr/lib/x86_64-linux-gnu/libc-2.31.so
165 7f8c081ac000-7f8c081af000 r--p 001e7000 08:05 1840863                    /usr/lib/x86_64-linux-gnu/libc-2.31.so
166 7f8c081af000-7f8c081b2000 rw-p 001ea000 08:05 1840863                    /usr/lib/x86_64-linux-gnu/libc-2.31.so
167 7f8c081b2000-7f8c081b6000 rw-p 00000000 00:00 0
168 7f8c081b6000-7f8c0824c000 r--p 00000000 08:05 1841929                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
169 7f8c0824c000-7f8c0833c000 r-xp 00096000 08:05 1841929                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
170 7f8c0833c000-7f8c08385000 r--p 00186000 08:05 1841929                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
171 7f8c08385000-7f8c08386000 ---p 001cf000 08:05 1841929                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
172 7f8c08386000-7f8c08391000 r--p 001cf000 08:05 1841929                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
173 7f8c08391000-7f8c08394000 rw-p 001da000 08:05 1841929                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
174 7f8c08394000-7f8c08399000 rw-p 00000000 00:00 0
175 7f8c083ab000-7f8c083ac000 r--p 00000000 08:05 1840650                    /usr/lib/x86_64-linux-gnu/ld-2.31.so
176 7f8c083ac000-7f8c083cf000 r-xp 00001000 08:05 1840650                    /usr/lib/x86_64-linux-gnu/ld-2.31.so
177 7f8c083cf000-7f8c083d7000 r--p 00024000 08:05 1840650                    /usr/lib/x86_64-linux-gnu/ld-2.31.so
178 7f8c083d8000-7f8c083d9000 r--p 0002c000 08:05 1840650                    /usr/lib/x86_64-linux-gnu/ld-2.31.so
179 7f8c083d9000-7f8c083da000 rw-p 0002d000 08:05 1840650                    /usr/lib/x86_64-linux-gnu/ld-2.31.so
180 7f8c083da000-7f8c083db000 rw-p 00000000 00:00 0
181 7ffc9f0e5000-7ffc9f106000 rw-p 00000000 00:00 0                          [stack]
182 7ffc9f16d000-7ffc9f170000 r--p 00000000 00:00 0                          [vvar]
183 7ffc9f170000-7ffc9f171000 r-xp 00000000 00:00 0                          [vdso]
184 ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0                  [vsyscall]

  

 

14464

C++和C程序内存占用对比分析

原文:https://www.cnblogs.com/tangtangworld/p/13466817.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!