On Linux systems, when a program is loaded into memory, the kernel establishes code snippets, data segments, and stack segments for the user process address space, which is used for dynamic memory allocation between the data segment and the stack segment. The member variables Start_code and End_code in the kernel data structure mm_struct are the starting and ending addresses of the process snippet, Start_data and End_data are the start and end addresses of the process data segment, and Start_stack is the process stack segment start address , START_BRK is the process dynamic memory allocation start address (the start address of the heap), and a BRK (the current last address of the heap), which is the current terminating address of the dynamic memory allocation.
650) this.width=650; "src=" Http://my.csdn.net/uploads/201207/22/1342931565_1627.png "alt=" 1342931565_1627.png "/ >
Each process can access a virtual memory space of 3G, but at the time of program compilation, it is not possible or necessary to allocate such a large space for the program, only allocate not large data segment space, the dynamic allocation of space in the program is allocated from this piece. If this space is not enough, the malloc function family (realloc,calloc, etc.) calls the SBRK function to move the lower bound of the data segment, and the SBRK function maps the virtual address space to memory for use by the malloc function under the management of the kernel.
#include <unistd.h>int brk (void *addr); void *sbrk (intptr_t increment);
description
brk sets the end of the data segment to The value specified by End_ Data_segment, when this value is reasonable, the system does has enough memory and The process does not exceed it max data size (see Setrlimit (2)).
SBRK increments the program's data space by increment bytes. Sbrk isn ' t a system call, it's just a C library wrapper. Calling Sbrk with an increment of 0 can is used to find the current location of the program 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 are set to Enomem.
Brief translation:BRK () and SBRK () change program breaks( program Break)The location.BRK Change the absolute position,sbrk Relative change position: >0 increases position, <0 shrinks position.
in general and simple case, using malloc () to apply the memory, after release, still return back to the original place, If you apply again to the same size memory area, it is still available from the 1th time. when the argument in Sbrk () is 0 o'clock, we can find the location of the program break. #sysctl-W kernel/randomize_va_space=0
BRK () is a very simple system call, simply changing the value of the member variable BRK of the MM_STRUCT structure. sbrk ( int incr) This function is used to increase the amount of space allocated to the calling program's data segment, increasing the space of incr bytes.
In the application, these two functions are not directly used, and are replaced by malloc (), mmap (), and other functions:
(1) malloc () This type of library function performs more efficiently. It is also important to note that when malloc () is used to allocate too much space, such as exceeding the 0X20FF8 constant (as in my system (FEDORA15), other systems may change), malloc no longer allocates space from the heap, but instead uses mmap () This system call looks for available memory space from the mapping area.
(2) The MMAP system call implements a more useful dynamic memory allocation function, which can map all or part of a disk file to the user space, and the operation of the process read-write file becomes the operation of read-write memory. The Do_mmap_pgoff () function in the Linux/mm/mmap.c file is the core of the MMAP system call implementation. The code for Do_mmap_pgoff () simply creates a new vm_area_struct structure and assigns the parameter of the file structure to its member variable m_file, and does not actually load the contents into memory.
One of the basic ideas of Linux memory management is to establish a physical mapping of this address only when actually accessing an address.
C Language Memory allocation method
(1) Allocation from a static storage area. Memory is allocated at the time of program compilation, and the entire running period of the program is present in this block. For example, global variables, static variables.
(2) Create on the stack. When executing a function, the storage units of local variables within the function can be created on the stack, which are automatically freed when the function is executed at the end. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but allocates limited memory capacity.
(3) Allocation from the heap, also known as dynamic memory allocation. When the program is running, it uses malloc or new to request any amount of memory, and the programmer is responsible for freeing the memory with free or delete. The lifetime of dynamic memory is determined by us and is very flexible to use
C Language and memory application related functions are mainly alloc,calloc,malloc,free,realloc,sbrk and so on. Where Alloc is requesting memory from the stack, so there is no need to release it. The memory allocated by malloc is in the heap and does not initialize the contents of the memory, so basically malloc calls the function memset to initialize this part of the memory space. Calloc will initialize this part of the memory, set to 0. ReAlloc, however, adjusts the size of the memory requested by malloc. The requested memory eventually needs to be freed through the function free. The SBRK is the size of the data segment;
Malloc/calloc/free is basically a C function library, which is not related to the OS. The C library internally uses a certain structure to save the current amount of available memory. If the size of the program malloc exceeds the space that is left in the library, then the BRK system call will be called to increase the available space first. And then allocate space. Free, the freed memory is not immediately returned to the OS, but remains in the internal structure. For example: BRK is similar to wholesale, a one-time application of large memory to the OS, and malloc functions like retail, to meet the requirements of the program runtime. This mechanism is similar to buffering.
Both the function 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 allocate:
void *malloc (size_t size);
The Calloc () function has two parameters, each of which is the number of elements and the size of each element, and the product of these two parameters is the size of the memory space to allocate:
void *calloc (size_t numelements,size_t sizeofelement);
If the call succeeds, the function malloc () and Calloc () will return the first address of the allocated memory space.
The main difference between the malloc () function and the calloc () function is that the former cannot initialize the allocated memory space, while the latter can. If the memory space allocated by the malloc () function has not been used, then each of these may be 0, and conversely, if this portion of the memory space has been allocated, deallocated, and redistributed, then there may be a variety of data left behind. That is, when a program that uses the malloc () function starts (the memory space has not been reassigned) to work, it may cause problems after a period of time (the memory space has been reassigned).
The Calloc () function initializes each bit of the allocated memory space to zero, meaning that if you allocate memory for an element of a character type or integer type, those elements will be guaranteed to be initialized to zero, and if you are allocating memory for elements of pointer type, these elements are usually (but not guaranteed) are initialized to null pointers, and if you are allocating memory for elements of a real type, then these elements may (only in some computers) be initialized to 0 of the floating-point type.
Another point of difference between the malloc () function and the calloc () function is that the calloc () function returns an array of an object, but the malloc () function returns only one object. To make it clear that a memory space is allocated for an array, some programmers choose the calloc () function. However, in addition to initializing the allocated memory space, the vast majority of programmers consider the following two function invocation methods to be indistinguishable:
Calloc (numelements, sizeofelement);
malloc (numelements *sizeofelement);
One point to explain is that in theory (according to the Ansic standard) the arithmetic operations of pointers can only be performed in a specified array, but in practice, many C programs break through this limitation even if the C compiler or translator follows this rule. Therefore, although the malloc () function does not return an array, its allocated memory space can still be used by an array (as is the case with the realloc () function, although it cannot return an array). In summary, when you choose between the Calloc () function and the malloc () function, you only need to consider whether you want to initialize the allocated memory space, regardless of whether the function can return an array.
A memory leak occurs when the program is running with malloc, but no free. Part of the memory is not used, but because there is no free, so the system thinks that this part of memory is still in use, resulting in continuous application of memory to the system, is the system memory is continuously reduced. However, A memory leak simply means that when the program is running, the OS will reclaim all of the resources when the program exits. Therefore, the proper re-start of the program, sometimes a bit of a function.
Linux memory allocation and BRK (), sbrk () principle and application