How to measure the process memory (basic concept)

来源:互联网 发布:deepin linux开3d特效 编辑:程序博客网 时间:2024/05/18 20:12

ReferenceLink:

HowMuch Linux Memory is Used by a Process?

How do YouReally Measure Linux Bloat?


Use /proc to identify process memory usage.

You can get that information from the kernel via the handy /procfilesystem. It's all indexed by process ID. Get the process id for the programyou're interested in via ps -- you want the number in the PID column. Forprograms like X and firefox that list multiple processes, start with thebiggest one (the one with the biggest numbers in the %CPU and %MEM columns).

Onceyou have your process ID, look in the /proc/PID directory:

 
$ ls -F /proc/4500
attr/            cwd@     limits     mountstats  personality  stat     wchan
auxv             environ  maps       net/        root@        statm
clear_refs       exe@     mem        oom_adj     sched        status
cmdline          fd/      mountinfo  oom_score   smaps        syscall
coredump_filter  fdinfo/  mounts     pagemap     stack        task/

For checking memory, the most useful files are statm, mapsandsmaps. None of these files has much documentation, but what thereis can be found inman5 proc.

statm

The statm file is terse:

 
$ cat /proc/4500/statm
62372 27178 6455 14 0 50434 0

These numbers represent:

·                total program size (virtual size)

·                resident set size

·                shared pages

·                text (code)

·                (unused on Linux)

·                data + stack

·                (unused on Linux)

The units are pages. Howbig is a page? getconfPAGESIZE will tell you. It's 4096 on my system.

maps and smaps

mapsshows a list of mapped memory regions associated with the process. The fieldsare:

·                address

·                perms

·                offset

·                dev

·                inode

·                pathname

The pathname is particularly interesting, since that tells you ifthat region of memory corresponds to a shared library or data file -- justpaging through the file can give you an idea what sort of resources the programis using.

smapsshows details of those mapped memory regions. The first line is a repeat of theinformation frommaps. In addition, you can get each region's(virtual) size, its resident set size, the amount of shared memory that's clean(hasn't been written to) versus dirty, and the amount of clean and dirtyprivate (non-shared) memory. Each region looks something like this:

 
08048000-080bc000 r-xp 00000000 03:02 13130 /bin/bash
Size:               464 kB
Rss:                424 kB
Shared_Clean:       424 kB
Shared_Dirty:         0 kB
Private_Clean:        0 kB
Private_Dirty:        0 kB

 

Viewing a Simple Process Snapshot with ps

The ps utility provides a snapshot of processes running onthe system, reporting the virtual and resident memory size, CPU and memorypercentages, and a wide variety of other information. To examine a singleprocess, use the following options to ps, including vsz for the virtual memorysize, and rss for the resident memory size. For example,

BJATCA01sta1:/proc/3105(8)# ps -p 3129 -o pid,rss,vsz,comm

  PID   RSS    VSZ COMMAND

 3129 23100 213664 GAM


How Does a Process Use Memory?

When a process starts, the operating system allocates theamount of memory the process requires to run. Each process has four memorysegments for executable code and data.

  • The "code" segment contains the executable instructions.
  • The "data" segment contains static and global variables.
  • The "stack" contains local variables. The "heap" contains dynamically allocated objects.
  • Processes may use shared libraries. Shared libraries are only loaded into memory once, and all processes using a particular shared library will use that copy.

What Memory StatisticsDoes Linux Report?

  • Virtual memory is the total amount of the address space reserved by the operating system to the process for its code, data and stack. The virtual memory size reported by Linux includes all the code, data and stack space reserved for use by a process, on both physical memory and swap space. This value includes space used by all shared libraries used by the process. The implication of this is that shared libraries count in full towards the size of a given process.
  • The resident memory size of a process reported by Linux includes only the amount of physical memory the process and the shared libraries it references are using at a given time.Segments moved to swap space are not included. Like with virtual memory size, resident memory size includes the space used by shared libraries.

 

The differenceamong VIRT, RES, and SHR in top output

  • VIRT stands for the virtual size of a process, which is the sum of memory it is actually using, memory it has mapped into itself (for instance the video card's RAM for the X server), files on disk that have been mapped into it (most notably shared libraries), and memory shared with other processes. VIRT represents how much memory the program is able to access at the present moment.
  • RES stands for the resident size, which is an accurate representation of how much actual physical memory a process is consuming. (This also corresponds directly to the %MEM column.) This will virtually always be less than the VIRT size, since most programs depend on the C library.
  • SHR indicates how much of the VIRT size is actually sharable memory or libraries). In the case of libraries, it does not necessarily mean that the entire library is resident. For example, if a program only uses a few functions in a library, the whole library is mapped and will be counted in VIRT and SHR, but only the parts of the library file containing the functions being used will actually be loaded in and be counted under RES.