Memory model for user space
When loading a program, the Linux operating system divides the memory used by the program into 5 segments: Text (program segment), data (segment), BSS (BSS data segment), Heap (heap), stack (stack).
Text Segment (Procedure section)
The text segment is used to store the program instruction itself, and when Linux executes the program, it loads the program's code into memory and puts it into the text segment. The memory of the program segment is located at the top of the memory of the entire program, and is fixed in length (because the code needs to be put in memory, the operating system is clear).
Data Segment (data segment)
Data segment is used to hold global variables and static variables that have been assigned in the code. Because the data type of such a variable (required memory size) and its value are determined in the code, data segment is next to the text segment and is fixed in length (the amount of memory required is already known beforehand).
Data is easier to understand than BSS, and its name implies that it is stored inside. Of course, if the data is all zero, the compiler treats it as a BSS for optimization purposes. In layman's words,data refers to non- const Global variables that have been initialized (not 0) . What is the characteristic of it, we still look at the performance of a small program.
int data_array[1024 * 1024] = {1};
int main (int argc, char* argv[])
{
return 0;
}
[Email protected] data]# gcc-g data.c-o Data.exe
[email protected] data]# LL
Total 4112
-rw-r--r--1 root root June 14:35 data.c
-rwxr-xr-x 1 root root 4200025 June 14:35 Data.exe
Simply by changing the initialized value to nonzero, the file becomes more than 4M. This shows that Data A global variable of type is a file space that occupies the runtime memory space.
BSS Segment ( BSS data segment)
The BSS segment is used to hold unassigned global variables and static variables. This piece is fixed in length next to the data segment.
BSS are those that are not initialized and initialized to 0 the global Variables
Int bss_array[1024 * 1024] = {0};
int main (int argc, char* argv[])
{
return 0;
}
[[email protected] bss]# gcc-g bss.c-o bss.exe
[[email protected] bss]# ll
Total
-rw-r--r-- 1 root root 84 June 14:32 bss.c
-rwxr-xr-x 1 root root 5683 June 14:32 bss.exe
variable bss_array size 4M , and the size of the executable file is only 5 K. Thus, BSS A global variable of type takes up only the memory space of the runtime and does not occupy a file space .
heap (heap)
This memory is used to store the dynamic memory space required by the program, such as using the malloc function to request memory space. is taken from the heap. This memory is next to BSS and the length is indeterminate.
Stack (stack)
Stack is used to hold local variables, when the program calls a function (including the main function), the value of some variables inside the function into the stack, After the function call finishes returning, the value of the local variable is not used, so out of the stack, the memory is given to another function of the variable use (the program executes, always in a function call inside).
Let's look at a description of the legend:
Program in-Memory mirroring