http://www.perfgeeks.com/?p=723 http://www.perfgeeks.com/?p=770
You often use the top command to understand process information, including information about memory. The command top Help document explains each field in this way.
Virt, Virtual Image (KB)
RES, resident size (KB)
SHR, Shared Mem size (KB)
%mem, Memory Usage (KB)
SWAP, swapped size (KB)
Code, code size (KB)
DATA, Data+stack size (KB)
Nflt, Page Fault count
NDRT, Dirty Pages count
Although there are notes, but still feel a little obscure, do not know what the meaning.
Process Memory Space
A program that is running, called a process. Each process has its own, independent, undisturbed memory space. This space is divided into several segments (Segment), namely text, Data, BSS, Heap, Stack. The user process memory space is also the VM (virtual memory) that the system kernel assigns to the process, but does not mean that the process consumes so much RAM (physical memory). How big the space is. The Virt value of the command top output tells us the size of each process memory space (the process memory space increases or shrinks as the program executes). You can also learn about the distribution of a process's memory space through/proc//maps, or pmap–d, for example:
#cat/proc/1449/maps
...
0012e000-002a4000 R-xp 00000000 08:07 3539877/lib/i386-linux-gnu/libc-2.13.so
002a4000-002a6000 r--p 00176000 08:07 3539877/lib/i386-linux-gnu/libc-2.13.so
002a6000-002a7000 rw-p 00178000 08:07 3539877/lib/i386-linux-gnu/libc-2.13.so
002a7000-002aa000 Rw-p 00000000 00:00 0
...
08048000-0875b000 R-xp 00000000 08:07 4072287/usr/local/mysql/libexec/mysqld
0875b000-0875d000 r--p 00712000 08:07 4072287/usr/local/mysql/libexec/mysqld
0875d000-087aa000 rw-p 00714000 08:07 4072287/usr/local/mysql/libexec/mysqld
...
PS: Linear address, access rights, offset, device number, inode, mapping file
VM allocation and release
"Memory is always occupied by the process", which can be understood in this way: The process always requires memory. When fork () or exec () is a process, system kernel will allocate a certain amount of VM to the process, as the memory space of the process, the size of the BSS segment, the data section of the defined global variables, static variables, text section of the character directly, the program itself memory image, etc. There is also the local variable decision for the stack segment. Of course, you can also dynamically allocate memory through functions such as malloc (), and extend heap up.
The biggest difference between dynamic and static allocation is: 1. It was not until run-time that the dynamic assignment was performed, and in Compile-time, it was decided how many text+data+bss+stack to allocate. 2. Dynamically allocated memory through malloc () requires the programmer to manually call free () to release memory, which can easily lead to memory leaks, while statically allocated memory is released after the execution of the process (Text, data), but the data in the stack section is very short and the function exits immediately destroyed.
We use several sample small programs to deepen our understanding
/* @filename: EXAMPLE-2.C * *
#include <stdio.h>
int main (int argc, char *argv[])
{
Char arr[] = "Hello World"; /* stack segment, rw---* *
char *p = "Hello World"; /* Text section, string direct quantity, r-x--* *
ARR[1] = ' l ';
* (++p) = ' l '; /* error, text paragraph can not write * *
return 0;
}
PS: variable p, which is in the stack segment, but what it refers to as "Hello World" is a string of direct quantities, placed in the text segment.
/* @filename: EXAMPLE_2_2.C * *
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Char *get_str_1 ()
{
Char str[] = "Hello World";
return str;
}
Char *get_str_2 ()
{
char *str = "Hello World";
return str;
}
Char *get_str_3 ()
{
Char tmp[] = "Hello World";
Char *str;