Heap and stack, and heapstack
The major differences between heap and stack are as follows:
1. Different management methods:
2. Different space sizes:
3. Different fragments are generated:
4. Different Growth directions:
5. Different assignments:
6. Different allocation efficiency:
7. Different access efficiency:
Management Method: For stacks, release is automatically managed by the program, without manual control in the program; For the heap, release is controlled by the programmer, and memory leak is easily generated.
Fragment generation: For the heap, frequent new/delete and malloc/free will inevitably lead to memory space disconnections, resulting in a large number of fragments and reduced program efficiency. For the stack, this problem will not exist, because the stack is an advanced and outgoing queue, they are so one-to-one correspondence, so it is impossible for a memory block to pop up from the middle of the stack, before the pop-up, the post-stack content on him has been popped up. For details, see> data structure. We will not discuss it one by one here.
Growth direction: For the stack, the growth direction is upward, that is, the direction to the memory address increase; For the stack, the growth direction is downward, is to increase towards memory address reduction.
Allocation Method: The heap is dynamically allocated, and the heap without static allocation is relatively flexible to control the survival time of the variable. There are two stack allocation methods: static allocation and Dynamic Allocation (local variables at runtime ). Static allocation is completed by the compiler, such as the allocation of global or static variables. Dynamic Allocation is implemented by the alloca function, but the stack dynamic allocation is different from the heap dynamic allocation. Its Dynamic Allocation is released by the compiler without manual implementation.
Allocation Efficiency: the stack is the data structure provided by the machine system. It will support the stack at the underlying layer: assign a special register to store the stack address, and each stack has a dedicated command execution, this makes stack access more efficient. The heap is provided by the C/C ++ function library (malloc actually calls sbrk, called by the mmap system). Its mechanism is very complicated, for example, to allocate a piece of memory, library functions search for available space in heap memory based on certain algorithms (for specific algorithms, refer to data structures/operating systems, if there is not enough space (probably because there are too many memory fragments), it is possible to call the system function to increase the memory space of the program data segment, so that there is a chance to allocate enough memory, then return. At the same time, because of the possibility of switching between the user State and the core state, the application for memory becomes more expensive. Obviously, the heap efficiency is much lower than the stack efficiency.
Space size: the stack application size is generally limited by the user who belongs to the program, and the stack application must be continuous memory space. If the current system has such large memory size, but it is not continuous, the application may also fail, while the heap is mainly limited by the memory size (physical memory and virtual memory ), whether or not the memory is continuous <the real maximum memory is affected by the sum of swap space and physical space>;
Allocate ownership: the stack memory owner is limited by the stack size of the program user. If the number of instances that have been started exceeds this limit, the core dump is triggered. The heap memory is limited by the memory size (max memory size). If the heap memory size is exceeded, new or malloc will fail to return. The maximum value (data seg size) in the data section of the program is the size of the Program Load. If the maximum value is exceeded, the program cannot be loaded, leading to startup failure.
Note:
Max memory size: The maximum resident set size.
Rss: resident set size, indicating the size of the RAM (memory) occupied by the process, in KB
Therefore, the stack is the most widely used in the program. Generally, function calls are also completed using the stack. The parameters and return addresses in the function call process are as follows,
Both EBP and local variables are stored in stacks. Therefore, it is recommended to use stacks instead of stacks for small and fixed memory.
Alias mytop = 'ps-eo rss, pmem, pcpu, pid, user, comm | sort-k 3-r-n | head'
Alias mytops = 'ps-eo rss, pmem, pcpu, pid, user, cmd | sort-k 3-r-n | head'