Linux Program runtime memory status and the corresponding viewing tool

Source: Internet
Author: User

recently, when solving a compilation problem, I have been thinking about a problem, that is, what is the state of memory when the executable program runs under Linux, and how to allocate memory and run it. Look at the information, to summarize, it is well known that the memory management under Linux is managed by Virtual storage, in allocating memory is not in the physical memory opened up a space, but in the use of the allocation, but also through the section page management. Compare the above nonsense, and start to see what the memory will be when the program runs.

In Linux the memory allocation is in the page unit, and the page is through the section management, each section is independent, facilitates the management. When a Linux program runs, it can be divided into the following memory segments:

The BSS segment (BSS segment) usually refers to an area of memory that is used to store uninitialized global variables in the program. BSS is the abbreviation for English Block Started by Symbol . BSS segments belong to static memory allocations.

This segment is used to store uninitialized global variables or global variables that are initialized to 0 by default, which does not occupy the size of program files, but consumes memory space when the program is running.

#define DEBUG "Debug" int space[1024][1024];int main () {  char *a = Debug;  return 1;}

The above declares a two-dimensional array of space, is a global variable, is not initialized, and the NM command allows you to view the symbol information in the program as follows:

0000000000600660 d _dynamic00000000006007f8 D _global_offset_table_0000000000400578 R _IO_stdin_used W _Jv _registerclasses0000000000600640 d __ctor_end__0000000000600638 D __ctor_list__0000000000600650 D __DTOR_END__ 0000000000600648 d __dtor_list__0000000000400630 R __frame_end__0000000000600658 D __jcr_end__0000000000600658 D __JCR _list__000000000060081c A __bss_start0000000000600818 D __data_start0000000000400530 t __do_global_ctors_ Aux00000000004003e0 T __do_global_dtors_aux0000000000400580 R __dso_handle W __gmon_start__000000000060063 4 D __init_array_end0000000000600634 d __init_array_start0000000000400490 t __libc_csu_fini00000000004004a0 t __libc_ Csu_init U [email protected] @GLIBC_2.2.5000000000060081c a _edata0000000000a00840 a _end0000000000400 568 T _fini0000000000400358 t _init0000000000400390 t _START00000000004003BC t call_gmon_start0000000000600820 b completed.63470000000000600818 W data_start0000000000600828 b dtor_idx.63490000000000400450 t frame_dummy0000000000400474 t main0000000000600840 B space 

The last line of B is a BSS segment, which means that space exists in the BSS segment.

The data segment is used to store the initialized global variables, the global variables initialized to 0 are optimized for compiling the strategy or are saved in the BSS section, the above program changes can be seen how the allocation.

#define DEBUG "Debug" int space[1024][1024];int data = 1;int No_data = 0;int Main () {  char *a = DEBUG;  return 1;}

After viewing with NM

0000000000600660 d _dynamic00000000006007f8 D _global_offset_table_0000000000400578 R _IO_stdin_used W _Jv _registerclasses0000000000600640 d __ctor_end__0000000000600638 D __ctor_list__0000000000600650 D __DTOR_END__ 0000000000600648 d __dtor_list__0000000000400630 R __frame_end__0000000000600658 D __jcr_end__0000000000600658 D __JCR _list__0000000000600820 A __bss_start0000000000600818 D __data_start0000000000400530 t __do_global_ctors_ Aux00000000004003e0 T __do_global_dtors_aux0000000000400580 R __dso_handle W __gmon_start__000000000060063 4 D __init_array_end0000000000600634 d __init_array_start0000000000400490 t __libc_csu_fini00000000004004a0 t __libc_ Csu_init U [email protected] @GLIBC_2.2.50000000000600820 a _edata0000000000a00840 a _end0000000000400 568 T _fini0000000000400358 t _init0000000000400390 t _START00000000004003BC t call_gmon_start0000000000600820 b completed.6347000000000060081c D data0000000000600818 W data_start0000000000600828 b dtor_idx.63490000000000400450 t frame_dummy0000000000400474 t main0000000000600830 b no_ data0000000000600840 B Space

You can see that the variable data is assigned to the data segment, and the no_data initialized to 0 is assigned to the BSS segment.

Iii.. Rodata Segment

This section is also called the constant area, for storing constant data, RO is the meaning of Read only. Note, however, that not all constants are placed in a constant data segment, and the special case is as follows:
1) Some immediate numbers are compiled with the instructions directly in the code snippet.

int main () {  int a = ten;  return 1;}
A is a constant, but it is not placed in the constant area, but is assigned directly by the immediate number in the instruction.



2) for string constants, the compiler removes duplicate constants, allowing only one copy of each string constant for the program.

Char *str = "123456789"; char *str1 = "HelloWorld"; int main () {  char* a = "HelloWorld";  Char b[10] = "HelloWorld";  return 1;}
The assembly code is as follows:

                . File "hello.c". Globl str. section. Rodata.        LC0:. String "123456789". Data. Align 8. Type str, @object. Size str, 8STR: . Quad. Lc0.globl str1. Section rodata.        LC1:. String "HelloWorld". Data. Align 8. Type str1, @object. Size str1, 8STR1: . Quad. LC1. TEXT.GLOBL Main. Type Main, @functionmain:. LFB0:. Cfi_startproc pushq%rbp. Cfi_def_cfa_offset cfi_offset 6, -16 movq%rs P,%rbp. Cfi_def_cfa_register 6 movq $.         LC1, -8 (%RBP) movl $1819043176, -32 (%RBP) movl $1919907695, -28 (%RBP) MOVW $25708, -24 (%RBP) MOVL $,%eax leave. CFI_DEF_CFA 7, 8 ret. Cfi_endproc. LFE0:. Size main,.-main. Ident "GCC: (GNU) 4.4.6 20110731 (Red Hat 4.4.6-3)". Section. E.gnu-stack, "", @progbits
You can see that str1 and a both point to the same LC1 in the. Rodata segment, but the string constants initialized with the array are not placed in the constant area, and the const-modified global variable is placed in the constant area, but the local variable that uses the Cons modifier is only set to read-only to prevent the effect of the modification, not into the constant area.

3) In some systems, the Rodata segment is shared by multiple processes in order to improve the utilization of space.

Iv. Text Segment

The text segment is used to store program code, compile-time OK, read-only. Further, it is the machine instructions for storing the processor, when the individual source files are compiled after the target file is generated, through the connector to link the various target files and to resolve the reference between the various source files, at the same time, all the target files in the. Text section together, but not simply "heap" together on the end, You also need to handle the function reference problem between the individual segments.

V. Stack segment

That is, one of the stack segments, often said, is the system is responsible for the release, the operation is similar to the stack, used to store parameter variables and local variables, in fact, the execution of the function is the way of the stack, so the recursion

Vi. the section of the heap

It is commonly known as the heap, which is applied and released by the user, at least allocated at the time of application, when the actual storage of data to allocate the corresponding real, release is not immediately released, but may be reused, and later will be more careful introduction of relevant knowledge.


You can see that the heap and stack are in the opposite direction of memory growth, followed by a detailed introduction to Linux memory management


Linux Program runtime memory status and the corresponding viewing tool

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.