Brk and sbrk.

Source: Internet
Author: User

 

The main task of brk and sbrk is to map virtual memory to memory. In GNUC, the memory allocation is as follows:

The virtual memory space accessible to each process is 3 GB. However, during program compilation, it is impossible or unnecessary to allocate such a large space for the program. Only a small data segment space is allocated, the dynamically allocated space in the program is allocated from this block. If this space is insufficient, the malloc function family (such as realloc and calloc) will call the sbrk function to move the lower bound of the data segment. The sbrk function maps the virtual address space to the memory under the kernel management, used by the malloc function. (See Linux kernel scenario analysis)

 

# Include <unistd. h>

Int brk (void * end_data_segment );

Void * sbrk (ptrdiff_t increment );

DESCRIPTION

Brk sets the end of the data segment to the value specified

End_data_segment, when that value is reasonable, the system does have

Enough memory and the process does not exceed its max data size (see

Setrlimit (2 )).

Sbrk increments the program's data space by increment bytes. sbrk

Isn' t a system call, it is just a C library wrapper. Calling sbrk

An increment of 0 can be used to find the current location of the pro-

Gram break.

RETURN VALUE

On success, brk returns zero, and sbrk returns a pointer to the start

Of the new area. On error,-1 is returned, and errno is set to ENOMEM.

 

 

Sbrk is a C library function instead of a system call. System calls usually provide a minimum interface, while library functions usually provide more complex functions.

 

In Linux, when a program is loaded into memory, the kernel creates code segments, data segments, and stack segments for the user's process address space. The idle areas between data segments and stack segments are used for dynamic memory allocation.

 

The member variables start_code and end_code in the kernel data structure mm_struct are the start and end addresses of the Process Code segment, start_data and end_data are the start and end addresses of the process data segment, and start_stack is the start address of the process stack segment, start_brk is the starting address for dynamic memory allocation of processes (the starting address of the heap), and a brk (the last address of the heap) is the ending address for dynamic memory allocation.

 

The basic function of dynamic memory allocation in C language is malloc (), and the basic implementation in Linux is called through the brk System of the kernel.

Brk () is a very simple system call, but simply changes the brk value of the member variable of the mm_struct structure.

 

Mmap system calls enable more useful dynamic memory allocation, which can map all or part of a disk file to the user space, the read/write operation of a process changes to the read/write memory operation.

The do_mmap_pgoff () function in the linux/mm/mmap. c file is the core of mmap system calling implementation.

Do_mmap_pgoff () Code only creates a vm_area_struct structure and assigns the file structure parameter to its member variable vm_file. The file content is not actually loaded into the memory.

One of the basic concepts of Linux memory management is that physical ing of an address is established only when an address is actually accessed.

 

 

//////////////////////////////////////// //

C language and Memory Allocation Method

(1) distribution from the static storage area. The program has been allocated when it is compiled, and the program exists throughout the entire runtime. For example, global variables and static variables.

(2) create a stack. When a function is executed, the storage units of local variables in the function can be created on the stack. When the function is executed, these storage units are automatically released. Stack memory allocation computation is built into the processor's instruction set, which is highly efficient, but the memory capacity allocated is limited.

(3) allocate from the stack, also known as dynamic memory allocation. When the program runs, it uses malloc or new to apply for any amount of memory. The programmer is responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by us. It is very flexible to use, but it has the most problems.

 

Functions related to memory application in C language include alloca, calloc, malloc, free, realloc, and sbrk.

Alloca applies for memory from the stack, so the memory allocated by. malloc does not need to be released and is located in the heap.

Initialize the memory content. Therefore, basically, after malloc, call the memset function to initialize the memory space.

Calloc initializes this part of memory and sets it to 0. While realloc is used to set the size of the memory applied for by malloc

Adjust. The applied memory needs to be released using the function free, while the sbrk increases the data segment size;

 

Malloc/calloc/free are basically implemented by the C function library, which is not related to OS.

Structure to save the current amount of available memory. If the size of the program malloc exceeds the size reserved by the Library, then

The brk system call will be called to increase the available space first, and then the released memory is not immediately allocated when the space is allocated. free

Return to the OS, but keep it in the internal structure. For example, brk is similar to a wholesale one-time application to the OS.

Memory, while malloc and other functions are similar to retail, to meet the requirements of running the program. This mechanism is similar to buffering.

 

The reason for using this mechanism: system calls cannot support memory allocation of any size (some system calls only support applying for memory of a fixed size and its multiples. In this case, the allocation of small memory will result in a waste; the system call request memory is expensive, involving the conversion of user and core states.

Both the functions malloc () and calloc () can be used to allocate dynamic memory space, but the two are slightly different.

The malloc () function has a parameter, that is, the size of the memory space to be allocated:

Void * malloc (size_t size );

The calloc () function has two parameters: the number of elements and the size of each element. The product of these two parameters is the size of the memory space to be allocated:

Void * calloc (size_t numElements, size_t sizeOfElement );

If the call is successful, both the malloc () and calloc () functions return the first address of the allocated memory space.

The main difference between the malloc () and calloc () functions is that the former Cannot initialize the allocated memory space, while the latter can. If the memory space allocated by the malloc () function has never been used before, each of them may be 0; otherwise, if the memory space has been allocated, released, and re-allocated, a variety of data may be left over. That is to say, when the program using the malloc () function starts (the memory space has not been re-allocated), it can run normally, but after a period of time (the memory space has been re-allocated) problems may occur.

The calloc () function initializes every bit in the allocated memory space to zero. That is to say, if you allocate memory for elements of the character or Integer type, these elements are always initialized to zero. If you allocate memory to elements of the pointer type, these elements are usually (but cannot be guaranteed) initialized to a null pointer; if you allocate memory for elements of the real number type, these elements (only in some computers) may be initialized to zero of the floating point type.

Another difference between the malloc () and calloc () functions is that the calloc () function returns an array composed of some objects, but the malloc () function returns only one object. To explicitly allocate memory space for an array, some programmers use the calloc () function. However, apart from whether to initialize the allocated memory space, most programmers consider the following two function call methods:

Calloc (numElements, sizeOfElement );

Malloc (numElements * sizeOfElement );

It should be explained that, theoretically (according to the ANSIC standard) arithmetic operations of pointers can only be performed in a specified array, but in practice, even if the C compiler or translator follows this rule, many C Programs still break through this restriction. Therefore, although the malloc () function cannot return an array, the memory space it allocates can still be used for an array (the same for the realloc () function, even though it cannot return an array ).

In short, when you select between the calloc () function and the malloc () function, you only need to consider whether to initialize the allocated memory space, instead of considering whether the function can return an array.

When the program runs malloc but is not free, memory leakage may occur. Some of the memory does not exist.

Because it is not free, the system considers that this part of memory is still in use, resulting in constant application to the System

But the memory leakage only means that when the program is running and the program exits, the OS will return

Collect all the resources. Therefore, restarting the program properly may be helpful.

Related Article

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.