Heap
Heap commit & decommit, 沒有保護動作, 如果覺得東西很重要請不要用heap用virtual memory
default heap size is 1 mb
/HEAP:reserve[,commit] commit的值是看OS的 reserve表示total heap allocate in virtual memory 最大可以占掉整個記憶體
/HEAP 選項會以位元組為單位設定堆積 (Heap) 的大小。 此選項只使用於建置 .exe 檔時。
reserve 引數會指定虛擬記憶體中堆積配置 (Heap Allocation) 的總計。 預設的堆積大小為 1 MB。 連結器會將指定的值進位至最接近 4 的倍數個位元組。
選擇性 commit 引數是隨作業系統的解讀而異。 在 Windows NT/Windows 2000 中,它是指定一次要配置的實體記憶體數量。 認可的虛擬記憶體會在分頁檔中保留空間。 當應用程式需要較多的堆積空間時,較高的 commit 值可以節省時間,但會增加記憶體需求且可能增加啟動時間。
Heap access是線性的, 一次只能有一個thread 存取, 但她可以有很多個heap default heap會跟著process的life cycle 用getProcessHeap來取的你的default heap
何時會需要額外的heap??
- Component protection
- 記憶體管理比較有效, 全部放在一起還需要整理空間, 有可能總大小夠但分散
- 記憶體存取區域化, 把node放在一起比較不會有swap page的機會
- heap的保護機制會有額外的負擔, 如果建很多heap就可以藉由分開存取來避免保護機制的處理
How to create additional heap: HANDLE HEAPCREATE( DWORD SIZE : initialize size SIZE : max size (0, heap_no_serialize: 效能好但其他thread可存取容易把heap搞壞掉, heap_generate_exceptions: 如果allocate fail會給你exception,可透過HeapSetInformation來增加保護機制, 有狀況OS會跳出來給你,但從此關不掉, heap_create_enable_execute: store executable code必須要開這個flag否則會跳access violation)
HeapAlloc flag: HEAP_ZERO_MEMORY, HEAP_GENERATE_EXCEPTION, HEAP_NO_SERIALIZE dwByte: 要多少byte 成功的話會回復address,失敗null
HeapCompatibilityInformation() //???? If you are allocating a lot of blocks with different sizes, the default algorithm used by the heap manager to handle the internal allocations could lead to address space fragmentation: it is not possible to find a free block of a given size because all the available blocks don't have the right size. Since Windows XP and Windows Server 2003, you can force the operating system to use a lowfragmentation heap algorithm for the memory allocation. In the case of multiprocessor machines, the performance of the low-fragmentation heap is greatly enhanced. Here is the code you need to write to switch to a low-fragmentation heap:
ULONG HeapInformationValue = 2;
if (HeapSetInformation(
hHeap, HeapCompatibilityInformation,
&HeapInformationValue, sizeof(HeapInformationValue)) {
// hHeap is turned into a low fragmentation heap
} else {
// hHeap can't be turned into a low fragmentation heap.
// Maybe because it has been created with the HEAP_NO_SERIALIZE flag
}
HEAP_REALLOC_IN_PLACE_ONLY: 不允許要其他更大的空間,
There can be no movement when reallocating a memory block. If this value is not specified, the function may move the block to a new location. If this value is specified and the block cannot be resized without moving, the function fails, leaving the original memory block unchanged.
Destroy BOOL HeapDestroy() default heap無法destroy只能跟著process life cycle
Using Heap with C++ 如果HeapCreate的max value=0表示無上限, 當delete的時候記得把heap設成null避免被用到
CSomeClass 如果 call這個,就會繼承heap & count等等的 子類別會和父類別共用
GetProcessHeaps To call GetProcessHeaps, you must first allocate an array of HANDLEs and then call the function as follows:
HANDLE hHeaps[25];
DWORD dwHeaps = GetProcessHeaps(25, hHeaps);
if (dwHeaps > 25) {
// More heaps are in this process than we expected.
} else {
// hHeaps[0] through hHeap[dwHeaps - 1]
// identify the existing heaps.
}
驗證heap的完整性: HeapValidate: 參數pvMem可以用來設定是否只檢查某個block, 0的話會檢查全部
HeapCompact: 回傳值是可以commit的largest block size in heap in byte. 但是size空間可能是不連續的
HeapLock & HeapUnlock 有lock起來就要等別人釋放才能用, 這兩個就是CS用的 HeapAlloc, HeapSize, HeapFree內建lock
HeapWalk: HEAP_Entry: 這個structor是存當前走的heap的資料, 第一次放null然後之後重複呼叫來走完