Malloc, sbrk, and mallocsbrk for Process Memory Management in Linux

Source: Internet
Author: User

Malloc, sbrk, and mallocsbrk for Process Memory Management in Linux

I was very interested in writing the malloc function. By the way, I would like to know about the memory management of processes. During the writing process, we found that malloc only allocates memory by calling the sbrk function in Linux, but adds a layer of memory management on the sbrk, sbrk and brk are ing from virtual memory to memory. Before writing it, let's take a look at the memory space allocation of the next process in Linux.


Process memory space allocation

In Linux, the virtual memory space allocated by each process is 3 GB, but it is impossible or unnecessary to allocate such a large space for a process in actual use. After all, memory is a very valuable resource. When a process is executed, the memory space allocated by the system mainly includes data segments, code segments, stacks, and stacks. The space applied by malloc is allocated from the heap. Let's take a look at the figure below:

This is the memory space of a process. Data Segment stores initialized static Data, while BSS segment stores initialized static Data, then the stack. It is worth noting that the growth direction of heap and stack is exactly the opposite. Now let's look at the distribution of data segment and BSS segment through a simple piece of code.

  8 #include <stdio.h>  9 #include <stdlib.h> 10  11 int bssvar; 18 int dataSegmentVar = 1; 19  20 int main() 21 { 22     printf("bssvar:%p, dataSegmentVar:%p,gap:%d", &bssvar, &dataSegmentVar,     ((int)&bssvar - (int)&dataSegmentVar)); 23     return 0; 24 }  
The running result is:

Bssvar: 0x6008c0, dataSegmentVar: 0x6008ac, gap: 20

We can see that dataSegment is under the BSS segment, and there are 20 bytes of space between them, that is, the size of the allocated data segment space is 20 bytes. However, the size is not fixed. If the static uninitialized variable in the program is greater than 20 bytes, the data segment space will increase accordingly.


  8 #include <stdio.h>  9 #include <stdlib.h>                                                                                                                                                               10  11 int bssvar, bssvar1, bssvar2, bssvar3, bssvar4, bssvar5; 12 char c; 13 int dataSegmentVar = 1; 14  15 int main() 16 { 17     printf("bssvar:%p, dataSegmentVar:%p,gap:%d", &bssvar, &dataSegmentVar, ((int)&bssvar - (int)&dataSegmentVar)); 18     return 0; 19 }
The running result is:

Bssvar: 0x6008c4, dataSegmentVar: 0x6008ac, gap: 24


At this time, the dataSegment variable is 5 int and a char, the total size is 21 bytes, And the dataSegment size is 24 bytes, the space exceeds 20, but to align, so it is 24 instead of 21.


In addition, it is worth noting that the program break is the end address of the Process heap. When you apply for a space through the malloc function, you actually use the sbrk function to move the program break so that it increases upwards to obtain a larger heap space. So the mysterious memory application is just moving a pointer, haha.

However, this is just a simple principle. There are still many details to consider. Next we will use a program.

  8 #include <stdio.h>  9 #include <stdlib.h> 10  11 int main() 12 { 13     void* ptr, *ptr1; 14     ptr = sbrk(0); 15     printf("sbrk:%p\n", ptr); 16     ptr1 = malloc(100); 17     ptr = sbrk(0); 18     printf("sbrk:%p, ptr1:%p\n", ptr, ptr1); 19     free(ptr1); 20     ptr = sbrk(0); 21     printf("sbrk:%p\n",ptr); 22 }~     

First, sbrk (0) is used in the program to obtain the end address of the heap part, and then malloc is used to apply for a 100-byte space, now let's look at the end address of the heap space and the address of the requested space. Finally, release the requested space and check the heap space address.

Program running result:

Sbrk: 0x2439000

Sbrk: 0x245a000, ptr1: 0x2439010

Sbrk: 0x245a000

At the beginning, the end address of the heap area is 0x2439000. However, after applying for a 100-byte space using malloc, the end address of the heap area changes to 0x245a000, which suddenly increases by 0x21000. In addition, it is worth noting that the starting address of the space requested by malloc is 0x2439010, which is 16 bytes backward than the ending address of the starting heap. This is not hard to understand. Every memory space requires some metadata to manage the space. Therefore, I guess these 16 bytes are used to record the information of the 100 bytes allocated by malloc, including the size and status.

So why did the program break move so much backward when I only applied for 100 bytes of space? This is not hard to understand. You cannot call the sbrk every time you apply for a small space. This is too costly. Therefore, we can allocate a large space at a time. In addition to the space applied by the user, the remaining space can be used for subsequent malloc space applications. Let's look at the next program:

  8 #include <stdio.h>  9 #include <stdlib.h> 10  11 int main() 12 { 13     void* ptr, *ptr1; 14     ptr = sbrk(0); 15     printf("sbrk:%p\n", ptr); 16     ptr1 = malloc(100); 17     ptr = sbrk(0); 18     printf("sbrk:%p, ptr1:%p\n", ptr, ptr1); 19     ptr1 = malloc(100); 20     ptr = sbrk(0); 21     printf("sbrk:%p, ptr1:%p\n",ptr, ptr1);                                                                                                                                       22     free(ptr1); 23     ptr = sbrk(0); 24     printf("sbrk:%p\n",ptr); 25 }

Running result:

Sbrk: 0x933000

Sbrk: 0x954000, ptr1: 0x933010

Sbrk: 0x954000, ptr1: 0x933080

Sbrk: 0x954000

As you can see, although the malloc function has applied for two 100-byte spaces, the program break has not been moved twice. In addition, the address difference between the first and second space is not 100 bytes but 112 bytes. The reason is probably because of alignment.

Through the above explanation, we found that, in fact, malloc is not so mysterious, it is just to use sbrk to apply for a space. However, in addition to applying for space, you also need to manage these space, which is the real core of malloc. These problems will be detailed in the next blog article "manually write malloc.

There are a lot of online usage of sbrk and brk. I will not elaborate on it here. I recommend an article: Click to open the link.


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.