Performance Analysis | Linux memory footprint analysis

This blog introduces the linux environment, view memory usage of two ways: using ps, top commands; see / proc / [pid] / file under. This article briefly describes the use of command and some parameters meaning, while the / proc / [pid] / content files made some detail. Article content from google and self-summary, if inappropriate, welcome criticism.

View Linux method memory

linux memory below to view a variety of channels, such as the command ps, top, free, pmap, etc., or via / proc system. Under normal circumstances, ps, top, pmap, free to meet the requirements, if you need more detailed and precise knowledge or memory of a whole process memory usage, through / proc system.

Use the command

free: display systems available memory, and memory information which has been used

ps: View process information, static, that is the current state

top: View process information, dynamic

pstree: View the process tree

pmap: ID view process information in accordance with the process

ps vs top

  1. ps command - a one-time snapshot of the system to provide information in the past, that is just the ps command to view the process of system information.
  2. The top command response is a dynamic system processes information, updated every 10s by default.
  3. ps and top are read from the process / proc directory status information, the kernel variety of useful information about the current system processes are placed in this pseudo-directory.

Common ps command:

ps -aux: View all processes

ps -l: Check into your bash related processes

Detailed top command, please refer http://www.jb51.net/article/40807.htm

Basic commands:

  • The keyboard number "1", to monitor the status of each logical CPU:
  • Keyboard "b" (Open / Close the highlight)
  • Keyboard "x" (open / close the highlight sort column)
  • "Shift +>" or "shift + <" may be the right or left to change the sort column
  • Tap display field "f" key, the basic layout view
  • s, change the screen update frequency
  • l, close or open a first portion of a first top line represents information
  • t, close or open a first part of the second row and third row Tasks information indicates Cpus
  • m, close or open the first part of the fourth row and the fifth row Mem information indicates Swap

Specific use, may be used man [cmd] view.

Related Parameters

VSZ & VIRT

  1. The total value of the virtual memory used by the process, including all code, data, shared libraries have been swapped out of. VIRT = SWAP + RES.
  2. VSZ from the ps command, VIRT from the top command, both expressed occupied by the process virtual memory size.
  3. If the application process memory 100m, but the actual use of only 10m, 100m then it will grow, rather than the actual usage

RES & RSS

  1. The process currently used memory size, but does not include swap out. RES = CODE + DATA.
  2. Other processes include sharing
  3. RES from the top command, RSS from the ps command, both of no difference in meaning representation, information is / [pid] / stat file read from / cat / proc.
  4. If the application memory 100m, 10m practical use, an increase of only 10m, and contrary VIRT
  5. About the case of libraries occupy memory, only statistically loaded library files share memory size

CODE

Physical memory size occupied by the executable code

DATA

  1. Physical memory size to store the data, the program runs in the need to use
  2. If the top command is not displayed, press the button to display the f

SHR

  1. Shared memory size
  2. In addition to its own process of shared memory, shared memory also contains other processes
  3. A process to calculate the physical memory size occupied by: RES - SHR
  4. After swap out, the values ​​will decline.

View the steps of a process memory information

1. Obtain the process PID

$ ps -aux | grep /usr/sbin/NetworkManager
root 845 0.0 0.0 387084 13332 ? Ssl 3月28 0:00 /usr/sbin/NetworkManager --no-daemon

2. Review the process of all threads

$ ps mp 845 -o THREAD,tid
USER %CPU PRI SCNT WCHAN USER SYSTEM TID
root 0.0 - - - - - -
root 0.0 19 - - - - 845
root 0.0 19 - - - - 1025
root 0.0 19 - - - - 1027

3. Check all child processes

$ pstree -p 845
NetworkManager(845)─┬─dhclient(30278)
├─dnsmasq(1123)
├─{gdbus}(1027)
└─{gmain}(1025)

Use of / proc file under

/ Proc / [pid] / the following documents related to the process of memory are mainly maps, smaps, status.

maps: File can view the code segment of a process, the stack area, stack area, dynamic libraries, kernel region corresponding to the virtual address

smaps: Each partition display more detailed memory usage data

status: active CPU contains all the information, all the values ​​in the file are cumulative from the beginning to the current system startup time

Famous and anonymous:

Files can be mapped to a region of memory processes, the mapping file descriptors stored in the vm_area_struct-> vm_file domain, this area of ​​memory called the known area of ​​memory, on the contrary, are anonymous mapped memory areas.

maps file analysis

Proc / [pid] / maps show the process of mapping the area of ​​memory and access rights. The set of operations corresponding to the kernel is proc_pid_maps_op, specific export function show_map. Some kernel address space is represented by a process vm_area_struct structure, all of the address space is stored in the task-> mm-> mmap list.

Line taken as follows:

7f4e3f5ca000-7f4e3f674000 r-xp 00000000 08:02 525202 /usr/lib/x86_64-linux-gnu/NetworkManager/libnm-device-plugin-wifi.so

Vm_area_struct each corresponding to resolve the following table:

Linux memory footprint analysis

maps files can only display a simple partition, smap files can display more detailed memory usage data for each partition.

smap file analysis

Interception of a file, each field interpreted as follows:

RW-P 08:02 00.026 million 7f148b2fa000-7f148b2fb000 2,883,675 /lib/x86_64-linux-gnu/ld-2.23.so 
Size: Size of virtual memory. 4 kB
Rss: 4 kB actual physical memory size RSS = Shared_Clean + Shared_Dirty + Private_Clean + Private_Dirty
Pss: 4 kB RSS in private memory pages
Shared_Clean: 0 kB RSS shared memory, not rewritten pages
Shared_Dirty: 0 kB RSS shared memory, rewritten page
Private_Clean: 0 kB RSS in private memory, not rewrite
Private_Dirty : 4 kB RSS in private memory is rewritten
Referenced: 4 kB
Anonymous: 4 kB
AnonHugePages: 0 kB
Shared_Hugetlb: 0 kB
Private_Hugetlb: 0 kB
swap: 0 kB page size in the exchange zone
SwapPss: 0 kB
KernelPageSize: 4 kB OS a page size
MMUPageSize: 4 kB page size MMU architecture
Locked: 0 kB
VmFlags: RD WR AC SD of Mr DW Mw of Me

Dirty next page if there is no case exchange mechanism, it should not be recycled.

Analysis of the script:

I wrote a simple analysis of the script, as follows, can be modified as needed.

#! /bin/bash
awk 'BEGIN{
total = 0;
printf("SIZE\tRSS\tSHARED_CLEAN\tSHARED_DIRTY\tPRIVATE_CLEAN\tPRIVATE_DIRTY\n")
}{
if(NF >3){
if($2 ~ /[r-][w-][x-][ps]/){
if($6 =="")
name = $1;
else
name = $6;
}
}
while(getline)
{
if(NF >3){
if($2 ~ /[r-][w-][x-][ps]/){
if($6 =="")
name = $1;
else
name = $6;
}
}
if($1 ~ /^Size/){
size = $2;
total += $2;
}

if($1 ~ /Rss/){
rss = $2;
}

if($1 ~ /Shared_Clean/){
shared_clean = $2;
}
if($1 ~ /Shared_Dirty/){
shared_dirty = $2;
}

if($1 ~ /Private_Clean/){
private_clean = $2;
}

if($1 ~ /Private_Dirty/){
private_dirty = $2;
}
if($1 ~ /VmFlags/){
printf("%d\t%d\t%d\t%d\t%d\t%d\t%s\n",size,rss,shared_clean,shared_dirty,private_clean,private_dirty,name);
size = 0;
name = "";
rss = 0;
shared_clean = 0;
shared_dirty = 0;
private_clean = 0;
private_dirty = 0;
continue;
}
}
}END{
printf("====total: %d\n", total);
}' $1

About anonymous mapping

There may be a large number of anonymous smaps area, which is generated using mmap mechanism, but is not associated with any file. Typically, they are mainly used to deal with some menial tasks, such as handling or shared memory buffer on the heap does not apply. For example pthread using anonymous mapping area as a new thread's stack, in Linux, a new thread pthread 8M application stack space as a space has a small on-chip memory (e.g., 4Kb) for the detection of memory overflow. So in each pthread is created, it is assigned a node 8Mb memory mapped to 0, and a map to the node 4Kb zone 0.

status file analysis

Intercepting files, parsing the following fields:

Develop> CAT / proc / 24475 / Status 
the Name: the name of the executable program netio
State: R (running) task status, run / sleep / dead
Tgid: 24475 thread group number
Pid: 24475 process the above mentioned id
ppid: 19635 parent the above mentioned id
TracerPid: 0
Uid: 0 0 0 0
Gid: 0 0 0 0
FDSize: 256 maximum number of file descriptors that the process
Groups: 0
VmPeak: 6,330,708 kB memory usage peak
VmSize: 268876 kB process virtual address space
VmLck: 0 kB process lock physical memory size, can not lock the physical memory to the hard disk
VmHWM: 16656 kB
VmRSS: 11420 kB process of physical memory in use
VmData: 230844 kB process data segment size
VmStk: 136 kB user mode process stack size
VmExe: 760 kB process code segment size
vmLib: 7772 kB process uses a library mapped into virtual memory space size
VmPTE: 120 kB process page table size
VmSwap: 0 kB
Threads: the number of the signal. 5 shared task descriptors, multiple lines POSIX application program, all threads in the same thread group using a signal descriptor.
SigQ: 0/63346 number of the signal to be processed
SigPnd: 0000000000000000 mask bits, the signal to be processed is stored the thread
ShdPnd: 0000000000000000 mask bits, the signal to be processed is stored that thread group
SigBlk: 0000000000000000 stored signal is blocked
SigIgn: 0000000001000000 stored signals are ignored
SigCgt: 0000000180000000 storage of captured signals
CapInh: 0000000000000000 ability inherited a program that can be executed by the current process
CapPrm: ability ffffffffffffffff process can be used, may contain ability CapEff not in, these capabilities are process their own temporary give up, CapEff is a subset of CapPrm, the process of giving up the ability not necessary to help improve security
CapEff: effective capacity ffffffffffffffff process
CapBnd: ffffffffffffffff
cpus_allowed: 01
Cpus_allowed_list: 0
Mems_allowed: 01
Mems_allowed_list: 0
voluntary_ctxt_switches: 201
nonvoluntary_ctxt_switches: 909

meminfo file analysis

File Machine memory usage / proc / meminfo, removal follows

Develop> CAT / proc / meminfo 
MemTotal: 8.11228 million kB all the available RAM size (i.e., physical memory minus some reserved bits in the binary code and kernel size)
MemFree: 4188636 kB LowFree and HighFree sum, the system is unused memory retention
buffers: 34728 kB used to make the file buffer size
Cached: 289740 kB is a cache memory (cache memory) memory size
(equal to minus swapCache diskcache)
SwapCached: 0 kB is a cache memory (cache memory) swap the size
has been swapped out of memory, but is still stored in the swapfile.
Used when needed soon to be replaced without the need to open the I / O port again
Active: 435240 kB buffer in active use or size of the cache page file,
unless it is very necessary otherwise they would not be used for other purposes
Inactive: 231512 kB in infrequently used or the size of the buffer cache memory page files, may be used in other ways.
the Active (anon): 361 252 kB
the Inactive (anon): 120 688 kB
the Active (file): 73 988 kB
the Inactive ( File): 110 824 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 0 kB total size of the swap space
SwapFree: 0 kB not use swap space size
Dirty: 0 kB waiting to be written back to disk memory size
Writeback: 0 kB is being written back to disk memory size
AnonPages: 348408 kB unmapped memory page size
Mapped: 33600 kB has been mapped devices and files such as the size of
Shmem: 133 536 kB
Slab: 55984 kB kernel data structures cache size can be reduced to bring the application and release memory consumption
SReclaimable: 25028 kB recoverable Slab size
SUnreclaim: 30956 kB irrecoverable Slab size (SUnreclaim + SReclaimable = Slab)
KernelStack: 1896 kB kernel stack area size
PageTables: 8156 kB paged memory management page index table size
NFS_Unstable: 0 kB instability in the page table size
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit : 2,483,276 kB
Committed_AS: 1804104 kB
VmallocTotal: 34,359,738,367 kB can vmalloc virtual memory size
VmallocUsed: 565680 kB has been using virtual memory size
VmallocChunk: 34,359,162,876 kB
HardwareCorrupted: 0 kB
HugePages_Total: 1536 a large number of pages
HugePages_Free: 0 Number of large pages free
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB a large page size
DirectMap4k: 10240 kB
DirectMap2M: 8302592 kB

to sum up

Under linux memory footprint is a more complex concept, not simply through a single indicator to judge a program "Memory consumption" size, because the memory of the application process will be used is not necessarily true (realization of malloc or mmap) and real It uses memory that the process is not necessarily only with themselves (such as dynamic shared libraries). We should choose the right way according to the specific needs to analyze memory usage.

Guess you like

Origin www.cnblogs.com/wyf0518/p/11456818.html