Five data segments involved in the Linux memory management process

Source: Internet
Author: User

 

A process (a program executed) occupies a certain amount of memory. It 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.

Five Linux Process segments

Next we will briefly summarize what the five different data zones in the memory space corresponding to the process are doing.

1) 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.

2) 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 and global variables allocated by the program.

3) BSS segment: the BSS segment contains uninitialized global variables in the program, and all bss segments in the memory are set to zero.

4) heap: a heap is used to store the memory segments dynamically allocated during running of 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)

Its physical memory space is applied by the program and released by the program.

5) 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 the static declared variable, 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.

It is allocated by the operating system, and the application and recovery of memory are all managed by the OS.

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.