标签:
Typically, in each process, the virtual memory available to that process is called its address space. Each process‘s address space is typically organized in 6 sections that are illustrated in the next picture: environment section - used to store environment variables andcommand line arguments; the stack, used to store memory for function arguments, return values, and automatic variables; the heap (free store) used for dynamic allocation, two data sections (for initialized and uninitialized static and global variables) and a text section where the actual code is kept.
Heap is the area of memory in which objects with dynamic extent are allocated. The lifetime of object is greater than the lifetime of the procedure that creates it. This object is said to have dynamic extent.
To understand why the dynamic memory allocation is time consuming let‘s take a closer look at what is actually happening. The memory area where new gets its blocks of memory for allocation (usually called free store or heap) is illustrated in the following picture:
When new is invoked, it starts looking for a free memory block that fits the size for your request. Supposing that such a block of memory is found, it is marked as reserved and a pointer to that location is returned. There are several algorithms to accomplish this because a compromise has to be made between scanning the whole memory for finding the smallest free block bigger than the size of your object, or returning the first one where the memory needed fits. In order to improve the speed of getting a block of memory, the free and reserved areas of memory are maintained in a data structure similar to binary trees called a heap. The various algorithms for finding free memory are beyond the scope of this article and you can find a thorough discussion about them in D. Knuth‘s monographThe Art of Computer Programming -- Vol.1, Fundamental Algorithms). This overhead combined with the risk for memory leaks makes the use of automatic memory (allocated on the stack) preferred whenever possible and the allocation is not large.
参考文献:
http://www.cprogramming.com/tutorial/virtual_memory_and_heaps.html
Process's address space and heap
标签:
原文地址:http://www.cnblogs.com/touchdown/p/5162781.html