美文网首页
进程所使用的内存空间

进程所使用的内存空间

作者: Ethan遗忘 | 来源:发表于2019-05-01 22:34 被阅读0次

    进程的内存空间布局

    进程的内存空间布局
    • Kernel space:内核态内存空间,用户代码无法读写,否则会导致“段错误”的异常
    • Random stack offset
    • Stack:用户态的堆栈,大小不固定,可以用 ulimit -s 进行调整,默认一般为 8M,从高地址向低地址增长
    • Random mmap offset
    • Memory Mapping Segment(mmap):共享内存,进程茫茫内存空间里的主要部分,既可以从高地址到低地址延伸(所谓 flexible layout),也可以从低到高延伸(所谓 legacy layout),看进程具体情况
    • Program break(brk)
    • Heap:程序运行过程中动态申请的内存
    • Random brk offset
    • BSS Segment:数据段,主要是进程里未初始化的全局数据总和,当然还有编译器生成一些辅助数据结构等等),大小取决于具体进程,其位置紧贴着代码段
    • Data Segment:数据段,主要是进程里已初始化的全局数据总和,当然还有编译器生成一些辅助数据结构等等),大小取决于具体进程,其位置紧贴着代码段
    • Text Segment(ELF, Executable and Linkable Format):代码段,主要是进程的指令,包括用户代码和编译器生成的辅助代码,其大小取决于具体程序

    一个C程序所使用的内存分析

    //main.cpp 
    int a = 0; 全局初始化区 
    char *p1; 全局未初始化区 
    main() 
    { 
    int b; 栈 
    char s[] = "abc"; 栈 
    char *p2; 栈 
    char *p3 = "123456"; 123456\0在常量区,p3在栈上。 
    static int c =0; 全局(静态)初始化区 
    p1 = (char *)malloc(10); 
    p2 = (char *)malloc(20); 
    分配得来得10和20字节的区域就在堆区。
    
     
    strcpy(p1, "123456"); 123456\0放在常量区。编译器可能会将它与p3所指向的"123456"优化成一个地方。 
    }
    

    相关概念

    • 虚拟内存 / Virtual Memory
    • 页表 / Page Table
    • 物理内存 / Physical Memory
    • 驻留内存 / Resident Memory
    • 共享内存 / Shared Memory

    查看进程的内存占用

    • top命令
    [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 
    
                           虚拟内存  驻留内存  共享内存    
      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     
    
    • 进程的maps文件
    [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
    
    # 数据段 / 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
    
    
    • 进程的smaps文件
    [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 
    

    参考链接

    相关文章

      网友评论

          本文标题:进程所使用的内存空间

          本文链接:https://www.haomeiwen.com/subject/csgwnqtx.html