進(jìn)程的內(nèi)存空間布局
- Kernel space:內(nèi)核態(tài)內(nèi)存空間榆俺,用戶代碼無(wú)法讀寫,否則會(huì)導(dǎo)致“段錯(cuò)誤”的異常
- Random stack offset
- Stack:用戶態(tài)的堆棧预烙,大小不固定墨微,可以用 ulimit -s 進(jìn)行調(diào)整,默認(rèn)一般為 8M扁掸,從高地址向低地址增長(zhǎng)
- Random mmap offset
- Memory Mapping Segment(mmap):共享內(nèi)存翘县,進(jìn)程茫茫內(nèi)存空間里的主要部分最域,既可以從高地址到低地址延伸(所謂 flexible layout),也可以從低到高延伸(所謂 legacy layout)锈麸,看進(jìn)程具體情況
- Program break(brk)
- Heap:程序運(yùn)行過(guò)程中動(dòng)態(tài)申請(qǐng)的內(nèi)存
- Random brk offset
- BSS Segment:數(shù)據(jù)段镀脂,主要是進(jìn)程里未初始化的全局?jǐn)?shù)據(jù)總和,當(dāng)然還有編譯器生成一些輔助數(shù)據(jù)結(jié)構(gòu)等等)忘伞,大小取決于具體進(jìn)程薄翅,其位置緊貼著代碼段
- Data Segment:數(shù)據(jù)段,主要是進(jìn)程里已初始化的全局?jǐn)?shù)據(jù)總和氓奈,當(dāng)然還有編譯器生成一些輔助數(shù)據(jù)結(jié)構(gòu)等等)翘魄,大小取決于具體進(jìn)程,其位置緊貼著代碼段
- Text Segment(ELF探颈, Executable and Linkable Format):代碼段熟丸,主要是進(jìn)程的指令训措,包括用戶代碼和編譯器生成的輔助代碼伪节,其大小取決于具體程序
一個(gè)C程序所使用的內(nèi)存分析
//main.cpp
int a = 0; 全局初始化區(qū)
char *p1; 全局未初始化區(qū)
main()
{
int b; 棧
char s[] = "abc"; 棧
char *p2; 棧
char *p3 = "123456"; 123456\0在常量區(qū),p3在棧上绩鸣。
static int c =0怀大; 全局(靜態(tài))初始化區(qū)
p1 = (char *)malloc(10);
p2 = (char *)malloc(20);
分配得來(lái)得10和20字節(jié)的區(qū)域就在堆區(qū)。
strcpy(p1, "123456"); 123456\0放在常量區(qū)呀闻。編譯器可能會(huì)將它與p3所指向的"123456"優(yōu)化成一個(gè)地方化借。
}
相關(guān)概念
- 虛擬內(nèi)存 / Virtual Memory
- 頁(yè)表 / Page Table
- 物理內(nèi)存 / Physical Memory
- 駐留內(nèi)存 / Resident Memory
- 共享內(nèi)存 / Shared Memory
查看進(jìn)程的內(nèi)存占用
[root@055e4e6dc6db /]# top
top - 21:31:26 up 18 min, 0 users, load average: 0.08, 0.08, 0.07
Tasks: 4 total, 1 running, 3 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 2047036 total, 132976 free, 322092 used, 1591968 buff/cache
KiB Swap: 1048572 total, 1048572 free, 0 used. 1540976 avail
虛擬內(nèi)存 駐留內(nèi)存 共享內(nèi)存
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 11784 2852 2560 S 0.0 0.1 0:00.12 bash
44 root 20 0 11784 3000 2576 S 0.0 0.1 0:00.05 bash
67 root 20 0 11784 3024 2596 S 0.0 0.1 0:00.23 bash
197 root 20 0 51912 3636 3112 R 0.0 0.2 0:00.00 top
[root@055e4e6dc6db /]# cat /proc/$$/maps
# 代碼段 / Text Segment
00400000-004dd000 r-xp 00000000 08:01 131482 /usr/bin/bash
006dc000-006dd000 r--p 000dc000 08:01 131482 /usr/bin/bash
006dd000-006e6000 rw-p 000dd000 08:01 131482 /usr/bin/bash
# 數(shù)據(jù)段 / BSS Segment + Data Segment
006e6000-006ec000 rw-p 00000000 00:00 0
# 堆 / Heap
01eaa000-01eec000 rw-p 00000000 00:00 0 [heap]
# mmap
7f1df94cc000-7f1df94d8000 r-xp 00000000 08:01 923657 /usr/lib64/libnss_files-2.17.so
7f1df94d8000-7f1df96d7000 ---p 0000c000 08:01 923657 /usr/lib64/libnss_files-2.17.so
7f1df96d7000-7f1df96d8000 r--p 0000b000 08:01 923657 /usr/lib64/libnss_files-2.17.so
7f1df96d8000-7f1df96d9000 rw-p 0000c000 08:01 923657 /usr/lib64/libnss_files-2.17.so
7f1df96d9000-7f1df96df000 rw-p 00000000 00:00 0
7f1df96df000-7f1df9897000 r-xp 00000000 08:01 923591 /usr/lib64/libc-2.17.so
7f1df9897000-7f1df9a97000 ---p 001b8000 08:01 923591 /usr/lib64/libc-2.17.so
7f1df9a97000-7f1df9a9b000 r--p 001b8000 08:01 923591 /usr/lib64/libc-2.17.so
7f1df9a9b000-7f1df9a9d000 rw-p 001bc000 08:01 923591 /usr/lib64/libc-2.17.so
7f1df9a9d000-7f1df9aa2000 rw-p 00000000 00:00 0
7f1df9aa2000-7f1df9aa4000 r-xp 00000000 08:01 923601 /usr/lib64/libdl-2.17.so
7f1df9aa4000-7f1df9ca4000 ---p 00002000 08:01 923601 /usr/lib64/libdl-2.17.so
7f1df9ca4000-7f1df9ca5000 r--p 00002000 08:01 923601 /usr/lib64/libdl-2.17.so
7f1df9ca5000-7f1df9ca6000 rw-p 00003000 08:01 923601 /usr/lib64/libdl-2.17.so
7f1df9ca6000-7f1df9ccb000 r-xp 00000000 08:01 133586 /usr/lib64/libtinfo.so.5.9
7f1df9ccb000-7f1df9ecb000 ---p 00025000 08:01 133586 /usr/lib64/libtinfo.so.5.9
7f1df9ecb000-7f1df9ecf000 r--p 00025000 08:01 133586 /usr/lib64/libtinfo.so.5.9
7f1df9ecf000-7f1df9ed0000 rw-p 00029000 08:01 133586 /usr/lib64/libtinfo.so.5.9
7f1df9ed0000-7f1df9ef1000 r-xp 00000000 08:01 923580 /usr/lib64/ld-2.17.so
7f1dfa0e9000-7f1dfa0ec000 rw-p 00000000 00:00 0
7f1dfa0ee000-7f1dfa0f1000 rw-p 00000000 00:00 0
7f1dfa0f1000-7f1dfa0f2000 r--p 00021000 08:01 923580 /usr/lib64/ld-2.17.so
7f1dfa0f2000-7f1dfa0f3000 rw-p 00022000 08:01 923580 /usr/lib64/ld-2.17.so
# Random mmap offset
7f1dfa0f3000-7f1dfa0f4000 rw-p 00000000 00:00 0
# 棧 / Stack
7ffd8e07b000-7ffd8e09c000 rw-p 00000000 00:00 0 [stack]
7ffd8e0a6000-7ffd8e0a8000 r--p 00000000 00:00 0 [vvar]
7ffd8e0a8000-7ffd8e0aa000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
# Random stack offset
# Kernel space
[root@055e4e6dc6db /]# cat /proc/1/smaps
00400000-004dd000 r-xp 00000000 08:01 131482 /usr/bin/bash
Size: 884 kB
Rss: 820 kB
Pss: 273 kB
Shared_Clean: 820 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 0 kB
Referenced: 820 kB
Anonymous: 0 kB
AnonHugePages: 0 kB
ShmemPmdMapped: 0 kB
Shared_Hugetlb: 0 kB
Private_Hugetlb: 0 kB
Swap: 0 kB
SwapPss: 0 kB
KernelPageSize: 4 kB
MMUPageSize: 4 kB
Locked: 0 kB
VmFlags: rd ex mr mw me dw
01a72000-01ab4000 rw-p 00000000 00:00 0 [heap]
Size: 264 kB
Rss: 208 kB
Pss: 208 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 208 kB
Referenced: 208 kB
Anonymous: 208 kB
AnonHugePages: 0 kB
ShmemPmdMapped: 0 kB
Shared_Hugetlb: 0 kB
Private_Hugetlb: 0 kB
Swap: 0 kB
SwapPss: 0 kB
KernelPageSize: 4 kB
MMUPageSize: 4 kB
Locked: 0 kB
VmFlags: rd wr mr mw me ac
7ffed73f5000-7ffed7416000 rw-p 00000000 00:00 0 [stack]
Size: 132 kB
Rss: 20 kB
Pss: 20 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 20 kB
Referenced: 20 kB
Anonymous: 20 kB
AnonHugePages: 0 kB
ShmemPmdMapped: 0 kB
Shared_Hugetlb: 0 kB
Private_Hugetlb: 0 kB
Swap: 0 kB
SwapPss: 0 kB
KernelPageSize: 4 kB
MMUPageSize: 4 kB
Locked: 0 kB
VmFlags: rd wr mr mw me gd ac
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Size: 4 kB
Rss: 0 kB
Pss: 0 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 0 kB
Referenced: 0 kB
Anonymous: 0 kB
AnonHugePages: 0 kB
ShmemPmdMapped: 0 kB
Shared_Hugetlb: 0 kB
Private_Hugetlb: 0 kB
Swap: 0 kB
SwapPss: 0 kB
KernelPageSize: 4 kB
MMUPageSize: 4 kB
Locked: 0 kB
VmFlags: rd ex
參考鏈接
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者