標籤:globalalloc heapalloc localalloc 記憶體配置
記憶體分配函數/記憶體管理API
參考:
Windows MSDN
http://msdn.microsoft.com/en-us/library/aa908768.aspx
附助資料:
http://blog.csdn.net/susubuhui/article/details/7315094
http://wenku.baidu.com/link?url=yxgCWePPV1kFaIUciEspYgm34wNAnMLDoduBlfsEEo-mW0JFRVEOkixomUjPatqw_jOXZcqQ1CLoeBSZqLuse1KiyHD6ysZTMIzLy_sPgPS
http://blog.csdn.net/sharecode/article/details/7464915
對於Windows來說,提供了一些API介面進行Heap記憶體管理,是獨立於C++/C程式之外的,僅用於Windows平台的API。
大概分為下面幾組介面:
老的Heap介面GlobalXXX(新程式不建議使用,這些函數存在主要是相容以前寫的程式):
GlobalAlloc/GlobalRelloc/GlobalFree:從Heap申請一塊記憶體
GlobalLock/GlobalUnlock: 從GlobalAlloc的記憶體中申請一段記憶體
函數參數:
新的Heap介面HeapXXX:
HeapCreate/HeapDestroy/GetProcessHeap:從Heap中申請一塊記憶體
HeapAlloc/HeapRelloc/HeapFree/HeapSize:從HeapCreate的記憶體中申請一段記憶體
HeapValidatee:查詢Heap或Alloc的資訊
當前進程Heap記憶體介面LocalXXX:
LocalAlloc/LocalReAlloc/LocalFree/LocalSize:從當前Heap的記憶體中申請一段記憶體,相當於從HeapAlloc從GetProcessHeap中申請記憶體。
虛擬位址記憶體介面: VirtualXXX
VirtualAlloc/VirtualFree/ VirtualProtect/VirtualQuery:從進程堆中申請頁面記憶體資料。通常用於申請一段大記憶體,最少申請一頁。reserves or commits a region of pages。
關於這一塊詳細請參考:http://blog.csdn.net/sharecode/article/details/7464915
malloc,new,VirtualAlloc,HeapAlloc這幾組的執行效率:
參考:http://blog.csdn.net/susubuhui/article/details/7315094
分配大記憶體的時候 VirtualAlloc才能體現出優勢,申請小記憶體時比較慢。
至於小塊記憶體那就是HeapAlloc > malloc > new,因為都是調用HeapAlloc,直接使用HeapAlloc要快很多。
擴充的新介面xxxEx(不是所有版本都支援)
以Ex結尾,例如VitualAllocEx,可以指定process handle,從而從其它運行進程空間申請一段記憶體。
函數說明:
HeapCreate:
從記憶體區申請一段記憶體作為堆。This function reserves memory from the shared memory area.
HeapDestroy:
刪除HeapCreate建立的堆。This function destroys the specified heap object.
GetProcessHeap:
擷取當前進程的堆Handle。注意這個不能使用Destroy刪除。This function obtains a handle to the heap of the calling process. This handle can then be used in calls to the HeapAlloc,HeapReAlloc,HeapFree, andHeapSize functions.
HeapAlloc:
從指定堆申請一段記憶體,該段記憶體是不被移動的。This function allocates a block of memory from a heap. The allocated memory is not movable.
HeapReAlloc:
從指定堆重新申請一段記憶體。This function reallocates a block of memory from a heap. The allocated memory is not movable.
HeapFree:
釋入HeapAlloc或HeapReAlloc申請的記憶體。This function frees a memory block from a heap. The memory block was allocated by the HeapAlloc or theHeapReAlloc function.
HeapSize:
根據Handle、記憶體開始地址,查詢一段HeapAlloc記憶體的大小。This function returns the size, in bytes, of a memory block allocated from a heap by the HeapAlloc orHeapReAlloc function.
HeapValidate:
檢查Heap Handle或Heap Handle下申請的記憶體是否有效。
This function validates the specified heap. HeapValidate scans all the memory blocks in the heap and verifies that the heap control structures maintained by the heap manager are in a consistent state. The HeapValidate function can also be used to validate a single memory block within a specified heap without checking the validity of the entire heap.
LocalAlloc/LocalReAlloc/LocalFree/LocalSize:
從當前進程Heap中進行操作。該組函數相當於HeapXXX指定Heap Handle為當前進程Handle。函數參考HeapAlloc / HeapReAlloc/ HeapFree/ HeapSize作用。
LocalAlloc : This function allocates the specified number of bytes from the heap.
In the linear Windows Embedded CE API environment, there is no difference between the local heap and the global heap.LocalAlloc is equivalent to HeapAlloc(GetProcessHeap, …).
VirtualAlloc/VirtualFree/ VirtualProtect/VirtualQuery:
從當前進程空間中申請頁面空間資料,最少申請一頁。
The VirtualAlloc function reserves or commits a region of pages in the virtual address space of the calling process. Memory allocated by this function is automatically initialized to zero, unless MEM_RESET is specified.
To allocate memory in the address space of another process, use theVirtualAllocEx function.
Win記憶體配置函數(GlobalAlloc/HeapAlloc/LocalAlloc/VirtualAlloc)