Memory distribution
Stack
First In, Last Out
queue
First In, First Out
The address location of each line of code in the memory unit
There is also a constant area, between HEAP and constant area
Heap
Dynamically allocate resources
- From the perspective of modern programming languages, using the heap, or using dynamic memory allocation, is a natural thing.
- Dynamic memory brings uncertainty: How long does it take to allocate memory? What if it fails? In occasions with high real-time requirements, such as some embedded controllers and telecommunications equipment.
- Generally speaking, when we allocate memory on the heap, many languages use keywords like new, and some languages use implicit allocation. The corresponding word for new in C++ is delete, because C++ allows programmers to completely take over the allocation and release of memory.
Principles of allocating and reclaiming dynamic memory
The program usually involves the operations of three memory managers:
- Allocate a memory block of a certain size
- Release a previously allocated memory block
- Garbage cleaning operation, looking for and releasing memory blocks that are no longer in use. This recycling strategy requires a unified and efficient approach to achieve a balance between performance, real-time performance, and additional overhead.
C++ did one and two things; Java did two things one and three.
Resource Management Plan-RAII
RAII:Resource Acquistion Is Initialization
- Relying on the stack and destructor to manage all resources-including heap memory. The use of RAII makes C++ not require garbage collection methods similar to Java, and can also effectively manage memory. The existence of RAII is also the main reason why garbage collection can be used in C++ in theory, but it has never been really popular.
- -RAII has some mature smart pointer representatives: such as std::auto_ptr and boost:shared_ptr
Comparison of several variables
Comparison of variables in stack and heap
Stack area | Heap area | |
---|---|---|
Scope | Function body, the scope of the statement block {} | Within the scope of the entire program, start with new, malloc and end with delete and free |
Size determination during compilation | Variable size range | Variable size range is uncertain and needs to be determined during runtime |
Size range | The default stack size of the Windows system is 1M, and the common default stack size of Linux is 8M or 10M (check with ulimit -s; the commands of different linux distributions are not guaranteed to be the same) | The upper limit of the heap space of all systems is close to the total size of the memory (virtual memory) (part of it is occupied by the OS) |
Memory allocation method | Address decreases from high to low | Address increases from low to high |
Is the content variable | variable | variable |
Variable comparison between global static storage area and constant storage area
Global static storage area | Constant storage area | |
---|---|---|
Store content | Global variables, static variables | constant |
Is the size determined during compilation | determine | determine |
Is the content variable | variable | Immutable |
Memory release
Memory leak
It means that the dynamically allocated heap memory in the program is not released or cannot be released for some reason, resulting in a waste of system memory, causing the program to slow down or even the system crashes and other serious consequences.
Causes of memory leaks
- Memory leaks mainly occur in the heap memory allocation method, that is, "after configuring the memory, all pointers to the memory are lost". Without a garbage collection mechanism like language, such memory cannot be returned to the system.
- Because the memory leak is a problem in the running of the program and cannot be identified by compilation, it can only be identified and diagnosed during the running of the program.