Linux proc 內存

ps:html

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內存,因爲程序中並無用到這些內存,沒有物理內存被分配,也就不該算到進程的賬上。 
node

進程的內存使用狀況比較複雜,這是由於: 
linux

    • 進程所申請的內存不必定真正會被用到
    • 真正用到了的內存也不必定是隻有該進程本身在用 (好比動態共享庫)

有效的實際使用內存 = 該進程獨佔的內存 + 共享的內存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區的大小和已經被使用掉的空間。
數據結構

 

mapsapp

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

address           perms offset  dev   inode   pathname
08048000-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. Here's a good article about it.

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 threadsspa

 

      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:<tid>] (since Linux 3.4)
                          A thread's stack (where the <tid> is a thread ID).
                          It corresponds to the /proc/[pid]/task/[tid]/
                          path.

                   [vdso] The virtual dynamically linked shared object.  See
                          vdso(7).

                   [heap] The process's heap.

              If the pathname field is blank, this is an anonymous mapping
              as obtained via mmap(2).  There is no easy way to coordinate
              this back to a process's source, short of running it through
              gdb(1), strace(1), or similar.

 

 

 

status:線程

 Develop>cat /proc/24475/status
Name:    netio   可執行程序的名字
State:    R (running) 任務狀態,運行/睡眠/僵死
Tgid:    24475  線程組號
Pid:    24475   進程id
PPid:    19635  父進程id
TracerPid:    0  
Uid:    0    0    0    0
Gid:    0    0    0    0
FDSize:    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
kB Threads: 5 SigQ: 0/63346 SigPnd: 0000000000000000 ShdPnd: 0000000000000000 SigBlk: 0000000000000000 SigIgn: 0000000001000000 SigCgt: 0000000180000000 CapInh: 0000000000000000 CapPrm: ffffffffffffffff CapEff: ffffffffffffffff CapBnd: ffffffffffffffff Cpus_allowed: 01 Cpus_allowed_list: 0 Mems_allowed: 01 Mems_allowed_list: 0 voluntary_ctxt_switches: 201 nonvoluntary_ctxt_switches: 909

 

meminfocode

下面是查看整機內存使用狀況的文件 /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 kB Active(file): 73988 kB Inactive(file): 110824 kB Unevictable: 0 kB Mlocked: 0 kB SwapTotal: 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 kB WritebackTmp: 0 kB CommitLimit: 2483276 kB Committed_AS: 1804104 kB VmallocTotal: 34359738367 kB 能夠vmalloc虛擬內存大小 VmallocUsed: 565680 kB 已經被使用的虛擬內存大小 VmallocChunk: 34359162876 kB HardwareCorrupted: 0 kB HugePages_Total: 1536 大頁面數目 HugePages_Free: 0 空閒大頁面數目 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB 大頁面一頁大小 DirectMap4k: 10240 kB DirectMap2M: 8302592 kB
相關文章
相關標籤/搜索