Memory leak and memory allocation interval

Memory leak:

When the dynamically allocated memory is not in use, it should be released so that the memory can be reused in the future. Allocating memory but not releasing it after use will cause memory leaks. Memory leak does not mean the physical disappearance of the memory, but after the application allocates a certain segment of memory, due to a design error, it loses control of the segment of memory before it is released, resulting in a waste of memory.

Memory allocation interval:

For a C language program, the memory space is mainly composed of five parts: code segment (.text), data segment (.data), static area (.BSS), heap and stack.
BSS segment : BSS segment (bss segment) usually refers to a memory area used to store uninitialized global variables and static variables in the program. BSS is the abbreviation of English Block Started by Symbol. The BSS segment belongs to static memory allocation. The BSS section does not contain any data, but simply maintains the start and end addresses, that is, the total size, so that the memory area can be allocated at runtime and be effectively cleared. The BSS section does not exist in the binary image file of the application, that is, it does not occupy disk space but only occupies memory space when running, so if global variables and static variables are not initialized, the executable file is much smaller.

Data segment : The data segment usually refers to a memory area used to store initialized global variables and static variables in the program. The data segment belongs to static memory allocation and can be divided into read-only data segment and read-write data segment. String constants, etc., but are generally placed in the read-only data section.

Code segment : Code segment/text segment usually refers to a memory area 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. Some architectures also allow the code segment to be writable, which means that the program can be modified. In the code segment, there may also be some read-only constant variables, such as string constants, etc., but they are generally placed in the read-only data segment.

Stack area : automatically allocated by the system, the allocation operation of the stack area is built into the processor's instruction set, and is automatically released by the system when the function execution ends. Store local variables. The disadvantage of the stack is: the capacity is limited, when the corresponding interval is released, the local variables can no longer be used. The command to query the stack capacity: ulimits -s. The stack is a contiguous area that extends to higher addresses, and the top and capacity of the stack are agreed upon in advance.

Heap area : It can be allocated during the execution of the program. It is determined by the programmer. The compiler cannot allocate space for them at compile time. It can only be allocated when the program is running, so it is called dynamic allocation. The heap is a non-contiguous area, which extends to higher addresses. Since the system uses a linked list to describe the free address space, the traversal of the linked list is from the ground address to the high address, so the heap area is a discontinuous dynamic storage space.

Guess you like

Origin blog.csdn.net/chaokudeztt/article/details/106084624