Article Title: Learn about Linux memory management from the developer's perspective. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Memory Management has always been the focus of all operating system books, regardless of whether the market or the Internet is filled with a large number of teaching materials and materials involving memory management. Therefore, the Linux memory management we want to write here adopts a lightweight strategy, so we will not go to the class from the theoretical level to make an axe and smile. What we want to do most is to talk about the memory management from the developer's perspective, the ultimate goal is to share our experience in using memory in kernel development and our understanding of Linux memory management.
How do processes use memory?
Without a doubt, all processes (executed programs) must occupy a certain amount of memory, which is used to store the program code loaded from the disk, or store data from user input. However, the Process manages these memories differently because of different memory usage. Some memories are allocated statically and centrally in advance, while others are allocated and recycled dynamically as needed.
For any common process, it involves five different data segments. A friend with a little programming knowledge can think of these data segments including "program code segments", "program data segments", and "program stack segments. Yes, these data segments are included, but in addition to the above data segments, the process also contains two data segments. Next we will briefly summarize the five different data zones in the memory space of the process.
Code segment: A code segment is an operation command used to store executable files. That is, it is an image of the executable program in the memory. The code segment must be prevented from being modified illegally at runtime. Therefore, only read operations are allowed, but write (modify) operations are not allowed-it cannot be written.
Data Segment: The data segment is used to store initialized global variables in the executable file. In other words, it is used to store static allocated variables and global variables of the program.
BSS segment: the BSS segment contains uninitialized global variables in the program, and all bss segments in the memory are set to zero.
Heap: a heap is used to store the memory segments dynamically allocated during a process. Its size is not fixed and can be dynamically expanded or reduced. When a process calls a function such as malloc to allocate memory, the newly allocated memory is dynamically added to the heap (the heap is expanded). When a function such as free is used to release the memory, released memory is removed from the heap (the heap is reduced)
STACK: the stack is a local variable temporarily created by the user to store the program. That is to say, the variable defined in the ARC "{}" of our function (but does not include static declared variables, static means to store variables in data segments ). In addition, when a function is called, its parameters will also be pushed into the process stack that initiates the call. After the call is completed, the return values of the function will also be stored in the stack. Because of the stack's first-in-first-out feature, the stack is particularly convenient for storing/restoring the call site. In this sense, we can regard the stack as a memory zone for storing and exchanging temporary data.
How do processes organize these regions?
Data segments, BSS, and heap in the above memory areas are usually stored continuously-the memory location is continuous, and code segments and stacks are often stored independently. Interestingly, the relationship between the stack and the stack is "ambiguous". One of them is "long" (stack down and stack up In the i386 architecture), and the other is "long ", relative. But you don't have to worry that they will meet each other, because they have a very large gap (in the end, you can calculate it from the example program below), and rarely have the chance to come together.
This section briefly describes the distribution of process memory areas:
498) this. style. width = 498; "border = 0>
We use a small example (derived from User-Level Memory Management) to show the differences and locations of the Memory areas mentioned above.
# Include
# Include
# Include
Int bss_var;
Int data_var0 = 1;
Int main (int argc, char ** argv)
{
Printf ("below are addresses of types of process's mem \ n ");
Printf ("Text location: \ n ");
Printf ("\ tAddress of main (Code Segment): % p \ n", main );
Printf ("________________________ \ n ");
Int stack_var0 = 2;
Printf ("Stack Location: \ n ");
Printf ("\ tInitial end of stack: % p \ n", & stack_var0 );
Int stack_var1 = 3;
Printf ("\ tnew end of stack: % p \ n", & stack_var1 );
Printf ("________________________ \ n ");
Printf ("Data Location: \ n ");
Printf ("\ tAddress of data_var (Data Segment): % p \ n", & data_var0 );
Static int data_var1 = 4;
Printf ("\ tNew end of data_var (Data Segment): % p \ n", & data_var1 );
Printf ("________________________ \ n ");
Printf ("BSS Location: \ n ");
Printf ("\ tAddress of bss_var: % p \ n", & bss_var );
Printf ("________________________ \ n ");
Char * B = sbrk (ptrdiff_t) 0 );
Printf ("Heap Location: \ n ");
Printf ("\ tInitial end of heap: % p \ n", B );
Brk (B + 4 );
B = sbrk (ptrdiff_t) 0 );
Printf ("\ tNew end of heap: % p \ n", B );
Return 0;
}
[1] [2] Next page