Linux C Process Memory layout

Source: Internet
Author: User

When a program file runs as a process, the process obtains space in memory. This space is the process of its own memory space. Each process space is divided into different regions as follows:

            

Process memory Space layout diagram

Text: Code snippet. Store all the code (instructions) of the program, derived from the code part of the binary executable file

initializeddata (referred to as data segment) and uninitialized( BSS ) Form a segment. Where The data segment holds the initialized global variable and the initialized static local variable, derived from the part of the binary executable file;

The BSS segment holds uninitialized global variables and uninitialized static local variables whose contents do not originate from the data portion of the binary executable (that is, the data part of the binary executable does not have uninitialized global variables and uninitialized Static local variable). According to the C language standard, their initial value must be 0, so the BSS segment is stored in a total of 0. The work of BSS Chingqing 0 is done by the system before the execution of the program after the binary file is loaded, and the system performs this 0 operation is done by a piece of code of the kernel, this code is the exec system call.

the stack (stack) holds dynamic local variables. That is, when a child function is called, the system allocates space from the stack to the dynamic local variable of the child function (note: The stack is extended to the memory low address), and when the child function returns, the stack of the system extends to the memory high address, which is equivalent to freeing the memory space of the dynamic local variable of the child function.

In the process of running a process, control is constantly transferred between functions by calling and returning functions. The process can be called at the time of the function, the original function of the frame is saved in the state we left, and for the new function to open the required frame space. When the calling function returns, the space occupied by the function's frame is emptied as the frame pops up. The process returns to the state saved in the frame of the original function and continues execution based on the instruction that the return address points to. The above process continues, the stack grows or decreases until main () returns, the stack is completely emptied, and the process ends.

When malloc is used in a program, the heap grows upward, and its growing portion becomes the space that malloc allocates from memory. The space created by malloc will persist until we use the free system to release it, or the process ends. A classic error is memory leakage, which means that we do not release heap space that is no longer being used, causing the heap to grow and the memory available space to be reduced.

The size of the stack and heap increases or decreases as the process runs. No memory is available when stacks and heaps grow to meet each other, that is, when the unused area in the memory space diagram disappears completely. The process will have stack overflow (stack overflow) errors, causing the process to terminate. In modern computers, the kernel typically allocates enough unused area areas for the process, and if the cleanup is timely, the stack overflow is easily avoided. Even so, the memory load is too large, there may be a stack overflow situation. We need to increase the physical memory.

The top end of the memory is the command line arguments and environment variables, the command line arguments and environment variables are placed in the specified location this operation is done by a piece of the OS code (theexec system call) after loading the binaries into memory and starting the program before running.

                

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.