博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux proc 内存
阅读量:2226 次
发布时间:2019-05-09

本文共 8717 字,大约阅读时间需要 29 分钟。

ps:

USER      PID    %CPU %MEM   VSZ   RSS  TTY  STAT  START  TIME  COMMAND 
root          4238     0.0        0.0     52396  352   pts/0    S       21:29    0:00   ./prog 

VSZ指的是进程内存空间的大小,这里是52396KB; 

RSS指的是驻留物理内存中的内存大小,这里是352KB。 
一般系统管理员知道VSZ并不代表进程真正用到的内存,因为有些空间会仅在页表中挂个名,也就是说只是虚拟存在着,只有真正用到的时候内核才会把虚拟页面和真正的物理页面映射起来。比如,prog.c中用malloc()分配的32MB内存,由于程序中并没有用到这些内存,没有物理内存被分配,也就不应算到进程的帐上。 

进程的内存使用情况比较复杂,这是因为: 

    • 进程所申请的内存不一定真正会被用到
    • 真正用到了的内存也不一定是只有该进程自己在用 (比如动态共享库)

有效的实际使用内存 = 该进程独占的内存 + 共享的内存A /共享A的进程数目 + 共享的内存B /共享B的进程数目 + ... 

free -m

total(96679)表示系统中物理内存总量。 
used(1631)表示已经分配的物理内存。 
free(95048)表示尚未分配的物理内存。 
shared(0)表示共享内存。 
buffers(196)表示分配给用作buffer的内存。 
cached(283)表示分配给用作cached的内存。 
第二行: 
-buffers/cache(1151): 第一行中的used - buffers - cached 
+buffer/cache(95528): 第一行中的free + buffers + cached 
说明:数据会有些许的误差,猜测是四舍五入引起的。 
-buffers/cache可以表示被进程实实在在消耗掉的内存。 
+buffers/cache可以表示还可以分配的内存大小。因为buffers/cache还可以被压缩。 
buffers和cache的区别: 
A buffer is something that has yet to be “written” to disk. A cache is something that has been “read” from the disk and stored for later use. 
第三行: 
交换区。当内存不够用的时候,系统会选择合适的进程,将其交换到swap区,把它占用的内存重新分配给其他进程。第三行表示swap区的大小和已经被使用掉的空间。

 

maps

Each row in /proc/$PID/maps describes a region of contiguous virtual memory in a process or thread. Each row has the following fields:

address           perms offset  dev   inode   pathname08048000-08056000 r-xp 00000000 03:0c 64593   /usr/sbin/gpm
  • address - This is the starting and ending address of the region in the process's address space
  • permissions - This describes how pages in the region can be accessed. There are four different permissions: read, write, execute, and shared. If read/write/execute are disabled, a '-' will appear instead of the 'r'/'w'/'x'. If a region is not shared, it is private, so a 'p' will appear instead of an 's'. If the process attempts to access memory in a way that is not permitted, a segmentation fault is generated. Permissions can be changed using the mprotect system call.
  • offset - If the region was mapped from a file (using mmap), this is the offset in the file where the mapping begins. If the memory was not mapped from a file, it's just 0.
  • device - If the region was mapped from a file, this is the major and minor device number (in hex) where the file lives.
  • inode - If the region was mapped from a file, this is the file number.
  • pathname - If the region was mapped from a file, this is the name of the file. This field is blank for anonymous mapped regions. There are also special regions with names like [heap][stack], or [vdso][vdso] stands for virtual dynamic shared object. It's used by system calls to switch to kernel mode. 

You might notice a lot of anonymous regions. These are usually created by mmap but are not attached to any file. They are used for a lot of miscellaneous things like shared memory or buffers not allocated on the heap. For instance, I think the pthread library uses anonymous mapped regions as stacks for new threads

 

The format of the file is:    address           perms offset  dev   inode       pathname    00400000-00452000 r-xp 00000000 08:02 173521      /usr/bin/dbus-daemon    00651000-00652000 r--p 00051000 08:02 173521      /usr/bin/dbus-daemon    00652000-00655000 rw-p 00052000 08:02 173521      /usr/bin/dbus-daemon    00e03000-00e24000 rw-p 00000000 00:00 0           [heap]    00e24000-011f7000 rw-p 00000000 00:00 0           [heap]    ...    35b1800000-35b1820000 r-xp 00000000 08:02 135522  /usr/lib64/ld-2.15.so    35b1a1f000-35b1a20000 r--p 0001f000 08:02 135522  /usr/lib64/ld-2.15.so    35b1a20000-35b1a21000 rw-p 00020000 08:02 135522  /usr/lib64/ld-2.15.so    35b1a21000-35b1a22000 rw-p 00000000 00:00 0    35b1c00000-35b1dac000 r-xp 00000000 08:02 135870  /usr/lib64/libc-2.15.so    35b1dac000-35b1fac000 ---p 001ac000 08:02 135870  /usr/lib64/libc-2.15.so    35b1fac000-35b1fb0000 r--p 001ac000 08:02 135870  /usr/lib64/libc-2.15.so    35b1fb0000-35b1fb2000 rw-p 001b0000 08:02 135870  /usr/lib64/libc-2.15.so    ...    f2c6ff8c000-7f2c7078c000 rw-p 00000000 00:00 0    [stack:986]    ...    7fffb2c0d000-7fffb2c2e000 rw-p 00000000 00:00 0   [stack]    7fffb2d48000-7fffb2d49000 r-xp 00000000 00:00 0   [vdso]              The address field is the address space in the process that the              mapping occupies.  The perms field is a set of permissions:                  r = read                  w = write                  x = execute                  s = shared                  p = private (copy on write)              The offset field is the offset into the file/whatever; dev is              the device (major:minor); inode is the inode on that device.              0 indicates that no inode is associated with the memory              region, as would be the case with BSS (uninitialized data).              The pathname field will usually be the file that is backing              the mapping.  For ELF files, you can easily coordinate with              the offset field by looking at the Offset field in the ELF              program headers (readelf -l).              There are additional helpful pseudo-paths:                   [stack]                          The initial process's (also known as the main                          thread's) stack.                   [stack:
] (since Linux 3.4) A thread's stack (where the
is a thread ID). It corresponds to the /proc/[pid]/task/[tid]/ path. [vdso] The virtual dynamically linked shared object. See . [heap] The process's heap. If the pathname field is blank, this is an anonymous mapping as obtained via . There is no easy way to coordinate this back to a process's source, short of running it through , , or similar.

 

 

 

status:

Develop>cat /proc/24475/statusName:    netio   可执行程序的名字State:    R (running) 任务状态,运行/睡眠/僵死Tgid:    24475  线程组号Pid:    24475   进程idPPid:    19635  父进程idTracerPid:    0  Uid:    0    0    0    0Gid:    0    0    0    0FDSize:    256 该进程最大文件描述符个数Groups:    0 VmPeak:     6330708 kB  内存使用峰值 VmSize:      268876 kB 进程虚拟地址空间大小 VmLck:           0 kB 进程锁住的物理内存大小,锁住的物理内存无法交换到硬盘 VmHWM:       16656 kB VmRSS:       11420 kB 进程正在使用的物理内存大小 VmData:      230844 kB 进程数据段大小 VmStk:         136 kB 进程用户态栈大小 VmExe:         760 kB 进程代码段大小 VmLib:        7772 kB 进程使用的库映射到虚拟内存空间的大小 VmPTE:         120 kB 进程页表大小 VmSwap:           0 kBThreads:    5SigQ:    0/63346SigPnd:    0000000000000000ShdPnd:    0000000000000000SigBlk:    0000000000000000SigIgn:    0000000001000000SigCgt:    0000000180000000CapInh:    0000000000000000CapPrm:    ffffffffffffffffCapEff:    ffffffffffffffffCapBnd:    ffffffffffffffffCpus_allowed:    01Cpus_allowed_list:    0Mems_allowed:    01Mems_allowed_list:    0voluntary_ctxt_switches:    201nonvoluntary_ctxt_switches:    909

 

meminfo

下面是查看整机内存使用情况的文件 /proc/meminfo

Develop>cat /proc/meminfo MemTotal:        8112280 kB 所有可用RAM大小 (即物理内存减去一些预留位和内核的二进制代码大小)MemFree:         4188636 kB LowFree与HighFree的总和,被系统留着未使用的内存Buffers:           34728 kB 用来给文件做缓冲大小Cached:           289740 kB 被高速缓冲存储器(cache memory)用的内存的大小                            (等于 diskcache minus SwapCache )SwapCached:            0 kB 被高速缓冲存储器(cache memory)用的交换空间的大小                              已经被交换出来的内存,但仍然被存放在swapfile中。                             用来在需要的时候很快的被替换而不需要再次打开I/O端口Active:           435240 kB 在活跃使用中的缓冲或高速缓冲存储器页面文件的大小,                               除非非常必要否则不会被移作他用Inactive:         231512 kB 在不经常使用中的缓冲或高速缓冲存储器页面文件的大小,可能被用于其他途径.Active(anon):     361252 kB Inactive(anon):   120688 kBActive(file):      73988 kBInactive(file):   110824 kBUnevictable:           0 kBMlocked:               0 kBSwapTotal:             0 kB 交换空间的总大小SwapFree:              0 kB 未被使用交换空间的大小Dirty:                 0 kB 等待被写回到磁盘的内存大小Writeback:             0 kB 正在被写回到磁盘的内存大小AnonPages:        348408 kB 未映射页的内存大小Mapped:            33600 kB 已经被设备和文件等映射的大小Shmem:            133536 kB Slab:              55984 kB 内核数据结构缓存的大小,可以减少申请和释放内存带来的消耗SReclaimable:      25028 kB 可收回Slab的大小SUnreclaim:        30956 kB 不可收回Slab的大小(SUnreclaim+SReclaimable=Slab)KernelStack:        1896 kB 内核栈区大小PageTables:         8156 kB 管理内存分页页面的索引表的大小NFS_Unstable:          0 kB 不稳定页表的大小Bounce:                0 kBWritebackTmp:          0 kBCommitLimit:     2483276 kBCommitted_AS:    1804104 kBVmallocTotal:   34359738367 kB 可以vmalloc虚拟内存大小VmallocUsed:      565680 kB 已经被使用的虚拟内存大小VmallocChunk:   34359162876 kBHardwareCorrupted:     0 kBHugePages_Total:    1536  大页面数目HugePages_Free:        0 空闲大页面数目HugePages_Rsvd:        0HugePages_Surp:        0Hugepagesize:       2048 kB 大页面一页大小DirectMap4k:       10240 kB DirectMap2M:     8302592 kB

 

转载于:https://www.cnblogs.com/SophiaTang/p/7635281.html

你可能感兴趣的文章
微软宣布加入 OpenJDK,打不过就改变 Java 未来!
查看>>
MyBatis动态SQL(认真看看, 以后写SQL就爽多了)
查看>>
为什么强烈推荐 Java 程序员使用 Google Guava 编程!
查看>>
先搞清楚这些问题,简历上再写你熟悉Java!
查看>>
【数据库】关系数据库和非关系数据库的优缺点
查看>>
【数据结构】动态顺序表
查看>>
Markdown的基础使用
查看>>
Linux基础命令
查看>>
【C语言】交换两个数值的三种方法
查看>>
【数据结构】栈的简单理解以及对栈的基本操作
查看>>
【数据结构】简单不带环迷宫的实现(用栈实现)
查看>>
【C语言】简单的了解递归(求斐波那契,n的阶乘,字符串长度,把一个整型(无符号),转化为字符型并打印出来)
查看>>
【数据结构】动态栈的实现
查看>>
【数据结构】简单的迷宫(用递归实现)
查看>>
【数据结构】队列的基本认识和队列的基本操作
查看>>
【数据结构】循环队列的认识和基本操作
查看>>
【LeetCode】无重复字符的最长子串
查看>>
时间复杂度
查看>>
【C++】动态内存管理 new和delete的理解
查看>>
【Linux】了解根目录下每个文件的作用
查看>>