Memory usage analysis of Linux processes

Source: Internet
Author: User
Tags kontact
How much memory does process XXX occupy? This is a frequently asked and often incorrect question. Memory allocation for Linux processes is a complex topic. Linux tools tend to oversimplify this issue, which leads to many misunderstandings and confusions. First, the ps and top tools are thrown away. how much memory does the process XXX occupy? This is a frequently asked and often incorrect question. Memory allocation for Linux processes is a complex topic. Linux tools tend to oversimplify this issue, which leads to many misunderstandings and confusions. First, you should discard tools such as ps and top, and then read such a simple program: [root @ pczou] # cat. /prog. c # I nclude # define ONEM (1024*1024) www.2cto.com int func () {char s [16 * ONEM]; char * p; p = malloc (32 * ONEM); pause (); return 0 ;}int main () {printf ("pid: % d \ n", getpid ()); func (); return 0;} among them, func () allocates 32 MB of memory and 16 MB of stack. Run the prog program and the prog will stop at the pause () location. let's see what ps says: user pid % CPU % mem vsz rss tty stat start time command root 4238 0.0 0.0 52396 352 pts/0 S. /prog VSZ refers to the size of the process memory space, which is 52366kb; RSS refers to the memory size residing in the physical memory, which is 352KB. Generally, the system administrator knows that VSZ does not represent the memory actually used by the process, because some spaces only have a name in the page table, that is, they only have virtual space, the kernel maps virtual pages to real physical pages only when they are actually used. For example, because the 32 MB memory allocated by malloc () in prog. c is not used in the program and no physical memory is allocated, it should not be included in the account of the process. The memory usage of the www.2cto.com process is complicated because: the memory applied by a process may not be used by the process itself (such as a dynamic shared library, the bill made by the restaurant is often full of loopholes, not counted in the amount of food that has not been taken, or the amount of money that has been paid for a single dish. Ps provides such a "confused" bill. The only way to clear the account is to repeat each dish and check whether there are any duplicates. The following Bill should be clear: Virtual memory: 52396 KB valid tive VM: 52120 KB Mapped: 352 KB valid tive mapped: 76.6 KB Sole use: 72 KB Per file memory use ld-2.3.4.so: VM 94208 B, M 90112 B, S 8192 B prog: VM 8192 B, M 8192 B, S 8192 B libc-2.3.4.so: VM 1180 KB, M 221184 B, S 16384 B can be seen, the actual a.ing (a.k. a. the allocated space is 352KB, which is consistent with the result given by ps. Let's look at the value of "Effective Mapped", which is only 76.6 KB. The calculation method of this value is: effective actual memory usage = exclusive memory of the process + shared memory A/shared A Process + shared memory B/shared B process +... for example, for a kde application kontact, the virtual address space of the Qt library is 7 M, and the actual ing space is 4.5 M, that is to say, the actual physical memory size allocated to Qt is 4.5 MB. Assuming that 10 KDE applications are running, the 4.5 M after the A-A should not be recorded on the kontact account. Although the calculation is not very accurate, the "valid tive Mapped" is enough to indicate the actual memory occupied by the process. Www.2cto.com OK. Finally, use this method to "settle accounts" for all processes in the system: as shown in the preceding statistics, although firefox occupies the largest virtual space, however, it actually occupies less memory than X Server. The actual memory used by firefox and its RSS (a.k. a. mapped) the difference is not big, accounting for 99% of RSS; while kontact actually occupies only 63% of RSS's memory, and 27% of memory is shared. From this we can roughly see that the window manager I use is KDE rather than Gnome, why? Because shared libraries such as Qt are shared by many KDE processes. The sole value can be understood as "private mapped", that is, the memory that may be released after the process exits (for non-anonymous ing pages, it may remain for a period of time ). Author railon
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.