Partitioning of memory areas

Source: Internet
Author: User

This article mainly explains the knowledge points about the memory partitioning of the application layer (c + + memory division), the Linux kernel layer (X86 system and ARM system).
First, the application layer
1. In C are divided into these storage areas: heap, stack, global zone (static zone), constant area
(1). Stack-the release is automatically assigned by the compiler. Stack, also known as the stack, is a local variable created by the user to temporarily create the program, that is, we function parentheses "{}" defined variables (but does not include the static declaration of the variable, static means to hold the variable in the data segment). In addition, when a function is called, its arguments are also pressed into the process stack that initiates the call, and the return value of the function is stored back to the stack when the call ends. Due to the advanced first-out features of the stack, the stack is particularly handy for saving/recovering call sites. In this sense, we can think of the stack as a memory area where temporary data is stored and exchanged.
(2). Heap-generally released by the programmer, if the programmer does not release, the program ends may be recycled by the operating system. A heap is a memory segment that is allocated dynamically during a process run and is not fixed in size and can be dynamically expanded or scaled down. When a process calls a function such as malloc to allocate memory, the newly allocated memory is dynamically added to the heap (heap is expanded), and freed memory is removed from the heap when memory is freed (heap is scaled down).
(3). Global zone (static zone), the storage of global variables and static variables is placed in a block, initialized global variables and static variables in one area, uninitialized global variables and uninitialized static variables in another area adjacent. -Program End Release
(4). There is also a special place for constants, called the constant area, that exists in the code snippet. -Program End Release

 from the above memory model we can see that there is a discrepancy with what we said above, it is not, we come, heap and stack area is no problem, The following is the global zone (static zone), which said that the uninitialized global variables and static variables are together, this area is called BSS, the initialization of global variables and static variables in a region, is in the data segment, the above BSS segment and data segment we can also call the data area, and finally the constant area, It is actually stored in the code snippet (read-only, when the program is loaded into the code snippet), so from the above model can also be divided into the C language of the following storage areas: heap, stack, data area, code snippet (personally think this division reasonable point). BSS segment: 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. In general, the BSS section will be zeroed out at initialization time. The BSS segment is a static memory allocation, where the program is zeroed out at the outset. Data segment: Data segment usually refers to an area of memory that is used to hold the initialized global variables in the program. The data segment belongs to static memory allocation. Code Snippets: Code segment/text segment usually refers to an area of memory that is used to store program execution code. The size of this area is determined before the program runs, and the memory area is usually read-only, and some schemas allow the code snippet to be writable, which allows the program to be modified. In a code snippet, it is also possible to include some read-only constant variables, such as String constants. The code snippet is the data that holds the program code, and if several processes in the machine run the same program, they can use the same code snippet. 

The variables defined in the function body are usually on the stack, and the allocated memory functions such as malloc, Calloc, realloc, etc. are allocated on the heap. The global amount is defined in the body of all functions, and after the static modifier is placed in the global zone (static zone), the static variable defined in the body of all functions is valid in the file and cannot be extern to another file. The static representation defined in the function body is valid only within the function body. In addition, a string such as "ADGFDF" in the function is stored in a constant area. Like what:
int a = 0; Global initialization Zone
Char *p1; Global uninitialized Zone
void Main ()
{
int b; Stack
Char s[] = "ABC"; Stack
Char *p2; Stack
Char *p3 = "123456"; 123456{post.content} in the constant area, p3 on the stack
static int c = 0; Global (static) initialization zone
P1 = (char *) malloc (10); Allocate 10 bytes of area in the heap area
P2 = (char *) malloc (20); Allocate 20 bytes of area in the heap area
strcpy (P1, "123456");
123456{post.content} is placed in a constant area and the compiler may optimize it with the "123456" that P3 points to
}

Application of BSS segment:
Compile two small programs with CL as follows:

Program 1:
int ar[30000];
void Main ()
{
......
}

Program 2:
int ar[300000] = {1, 2, 3, 4, 5, 6};
void Main ()
{
......
}

发现程序2编译之后所得的.exe文件比程序1的要大得多。原因就是,一个位于.bss段,而另一个位于.data段,两者的区别在于:全局的未初始化变量存在于.bss段中,具体体现为一个占位符;全局的已初始化变量存于.data段中;而函数内的自动变量都在栈上分配空间。.bss是不占用.exe文件空间的,其内容由操作系统初始化(清零);而.data却需要占用,其内容由程序初始化,因此造成了上述情况。

This ultimately explains:
The 1.BSS segment (data that has not been manually initialized) does not allocate space for the segment's data, but only the amount of space required to record the data.
2.data (data that has been manually initialized) segment is the data allocation space and the data is saved in the destination file.
Both the 3.text and data segments are in the executable file (typically cured in an embedded system in an image file) and are loaded from the executable by the system, while the BSS segment is not in the executable file and is initialized by the system.

2. In C + +, memory is divided into 5 zones, which are heap, stack, free storage, global/static storage, and constant storage, respectively.
(1). Stacks, which are the stores of variables that are assigned by the compiler when needed, and that are automatically clear when not needed. The variables inside are usually local variables, function parameters, and so on.
(2). Free storage, which is the memory blocks allocated by new, their release compiler does not go to the tube, by our application to control, generally a new will correspond to a delete. If the programmer does not release it, the operating system will automatically recycle after the program finishes.

is C + + Free storage equivalent to heap?
(3). Heap, which is the memory blocks allocated by malloc, is very similar to the heap, but it uses free to end his life.
(4). Global/static storage, global variables and static variables are allocated in the same piece of memory, in the previous C language, the global variables are divided into initialized and uninitialized, in C + + There is no such distinction, they occupy the same chunk of memory.
(5). Constant storage, which is a special piece of storage, they are stored in a constant, not allowed to modify (of course, you have to pass the improper means can also be modified)
Heap on the difference between free storage:
Free storage is an abstraction of the dynamic allocation and deallocation of objects through new and delete in C + +, which is the term for C and operating systems, and is a dynamically allocated memory that is maintained by the operating system.
The area of memory requested by new is called a free store in C + +. If free storage is implemented by the heap, it can be said that the memory area requested by new is on the heap.
Heaps are still different from free storage, and they are not equivalent because free-stored memory allocations are not necessarily allocated from the heap.
The New/delete implementation is an operator, and the implementation of Malloc/free is a library of functions, obviously different equivalents.
Specific analysis: http://blog.jobbole.com/109562/

Analysis: The difference between heaps and stacks.

Second, kernel-level memory allocation.
1.x86: Divide the 1g kernel virtual memory address into 4 regions: Direct Memory mapped area dynamic memory mapped area permanent memory mapped area fixed memory map area
Direct Memory Map area mapping relationship: In kernel initialization (not dynamic mapping), if physical memory is greater than 896MB (1g), Kernel 1g virtual address 896MB virtual memory and physical one by one mapping; 1g of virtual memory remaining 128m,
Start address: 0xc000000 size If the physical memory is greater than 1g, the size of the direct memory area is 896MB, if the physical memory is less than 896MB, Then the size of the physical memory is the size of the direct memory mapped area alias: Low-end memory
Dynamic memory Map Area mapping Relationship: If you need a physical address (physical memory address register address), only the dynamic establishment of physical address and dynamic memory Map area mapping (page table), you can Each time the program accesses the virtual address of the dynamic memory map of the kernel, it will eventually access the corresponding physical address (MMU), and if the corresponding physical address is not used, the mapping relationship must be de-mapped.
Start Address: As the size of the physical memory changes, if the physical memory is less than 896MB, such as 512M, the starting address =0xc000000+ physical memory size, if the physical memory is greater than 896MB, the starting address 0XC0000000+896MB, The default size is 12MB.
Persistent Memory map area: Mapping Relationship: It is also the physical address and kernel virtual address of the dynamic mapping, but if at some point, access to the physical address of the frequency is very large, if the page table is frequently established, then the access efficiency is not high, so you can map this physical address to the permanent memory map area, Once the mapping relationship is established, it is no longer necessary to destroy the corresponding mappings and speed up the access of the address. Of course, this mapping relationship can be destroyed artificially. Using the Kmap function for mapping, this mapping is likely to hibernate, so you can no longer interrupt context usage. Size: 4M
Fixed Memory map area: The purpose of the permanent memory map area is the same, the difference is only the fixed memory map area of the virtual address mapping, you can use in the context of the interrupt, the kernel preemption is forbidden.
User Virtual Address division: Stack mmap map area heap BBS data segment code Snippet
Note: The Direct memory map area is also called low-end memory:
Dynamic Memory Map area + Permanent Memory map area + Fixed memory map area = high-end memory;

2.ARM system: The partition of the kernel virtual memory area of 1G: 5 regions. Exception vector table (vetor), fixed memory map area, DMA memory map area, dynamic memory map area, direct memory map area. Note: The exception vector table of the ARM architecture itself is at the entry address 0xffff0000–0xffff1000 and 0 addresses. 3g of user space and physical memory access needs to be accessed in the dynamic map area of the kernel.
S5PV210 Processor 1G Kernel virtual address division: The Linux kernel initiates the printing of kernel information:
Virtual Kernel Memory layout:
vector:0xffff0000-0xffff1000 (4 KB)//Exception vector table entry address
fixmap:0xfff00000-0xfffe0000 (896 KB)//fixed memory map area
dma:0xff000000-0xffe00000 (MB)//DMA memory map
vmalloc:0xf4800000-0xfc000000 (+ MB)//dynamic memory map area
lowmem:0xc0000000-0xf4000000 (832 MB)//low-side memory (direct Memory map area)
modules:0xbf000000-0xc0000000 (MB)
. init:0xc0008000-0xc003a000 ($ KB)
. text:0xc003a000-0xc09ad000 (9676 KB)
. DATA:0XC09AE000-0XC0A15D20 (416 KB)
When the kernel is booted, the kernel's 1g memory map is initialized, and the page table initialization is placed in memory.
Virtual address = (Physical address – The starting address of the physical address) + 0xC0000000;

---------------------This article from the Embedded Youth CSDN blog, full-text address please click: 77119939?utm_source=copy

Partitioning of memory areas

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.