in a computer system, the data for the running application is kept in memory, and different types of data are saved in a region that has a total of five memory areas in the application: (1), stack "stack": Automatically allocated and released by the compiler, general storage function parameter values, local variables, etc. (2), heap area "heap": the programmer allocates and frees memory, if the programmer does not release, the program ends, may be recycled by the operating system (3), the global zone, "static": the storage of global variables and static variables are put together, and the area is divided into two cases, one is initialized, the other is uninitialized, the initialized global variables and static variables are stored in an area, Uninitialized global variables and static variables are released by the system after the end of the program in another adjacent area (4), literal constant area: Store A constant string, the system is released after the end of the program(5), program code area: the binary code that holds the functionThe global zone, the constant zone, and the code area are loaded into memory as the application starts, and will not be released until the application is destroyed, so we should focus on the stack area and the heap.
first, the memory of the application methodstack: Automatically assigned by the systemheap: Requires programmers to use alloc,new and other operational applications
second, the response of the system after applicationstack: Memory is allocated to the system if the remaining space on the stack is greater than the requested space, otherwise the exception prompt stack overflow will be reportedHeap: The allocation of heap memory goes through three steps, iterates through the list, finds and allocates the first block of space larger than the requested size for the program, and allocates the remaining space for recycling. (1), the operating system has a record of the free memory of the linked list, when the system receives the application of the program will traverse the list;(2) The system will look for the heap node in the list where the first space is larger than the requested space, then remove the node from the list of idle nodes and assign the space of that node to the program(3), because the size of the found heap node is not necessarily exactly equal to the size of the request, the system will automatically re-put the remaining portion of the idle list.
third, the size of the application limitstack: Stack memory allocation is a high address to the low address extension of the data structure, is a contiguous memory area, the top of the stack address and the maximum capacity of the stack is the system predetermined, that is, each allocation of memory size is determined by the system. Heap: The allocation of heap memory is the data structure of the low address to high address extension, is a discontinuous memory area, this is because the system uses the linked list to store the free memory address, nature is discontinuous, and the list is from the low address to the Highland address traversal, the heap size is limited by the effective virtual memory in the computer system. This shows that the heap obtained more flexible space, but also relatively large.
iv. efficiency of applicationstack: Allocated by the system, fast speedheap: Is allocated by alloc memory, slow and prone to memory fragmentation, but the most convenient to use
v. Contents of storagestack: variable name (without *) is equivalent to a pointer alias that points to the stack dataHeap: To access the data in the heap, it must be done by pointers, and the type of the pointer is how to access the data in the heap(1), the operating system in an anonymous way to record the allocated memory area, that is, only the memory address and size, do not record the specific type;(2), when a memory area is no longer in use, the program needs to notify the operating system to reclaim the memory area, so that the memory area can be reused by other programs, otherwise the area will never be re-allocated, this is the memory leak;(3), if an area has been released but still trying to access the area, you will be prompted "bad access", that is, "poor access", also known as "wild pointer access."
Memory allocation of stacks and heaps in the profiler