轉自:http://www.cnblogs.com/wangkewei/archive/2009/07/22/1528892.html 這篇文章是繼文章Windows Embedded CE 6.0 Internals (1)的。內存這塊一直是讓人頭痛的東西,因為比較復雜,但是我們卻需要經(jīng)常與其打交道——內存泄漏、異常定位、程序優(yōu)化等等。這篇文章以及后續的文章我試著(zhù)能夠刨根問(wèn)底。 5.內存構架內存的種類(lèi)1.Random Access Memory (RAM) Random access memory can be read or written directly at any address. There are various types of RAM that are differentiated by the underlying hardware technology used to implement them. However they all share the ability to be read or written directly at any random address. RAM memory is volatile, the contents are only maintained as long as power is not lost. 2.Read Only Memory (ROM) Read Only Memory typically refers to memory that can be read just like RAM, but not written directly. Most ROM used today can be rewritten using a software algorithm, and is often called Flash ROM. Data stored in ROM is nonvolatile, and remains valid even after power is removed. ROM is sometimes used to refer to any kind of memory that is nonvolatile, even if it is not randomly accessible (e.g. paged memory). 3.Paged Memory Some memory technologies are not randomly accessible. These memory technologies must be read and written in blocks. A CPU typically requires memory that it executes code from to be randomly accessible, so Paged memory can’t be used as a medium to execute code. This type of memory is often used for bulk nonvolatile storage, and includes NAND technology.(存儲在NAND Flash中的鏡像在執行時(shí)被拷貝到RAM中。而NOR Flash是支持XIP的,這也是NAND和NOR的主要區別。) 虛擬內存架構1.虛擬內存Windows Embedded CE 6.0使用單一的32位(4G)平坦模式虛擬內存尋址空間。 這樣可以高效的、保護的使用物理內存。 2.虛擬尋址有幾個(gè)概率需要澄清,首先是▲物理內存是被內存管理單元(MMU)擁有的,當然處理器得有MMU,不然沒(méi)法跑起來(lái)CE 6.0。 ▲把虛擬內存轉換為物理內存是MMU的工作。 ▲一個(gè)有效的虛擬內存必須是已經(jīng)映射到物理內存的。 ▲操作系統中近乎所有地址都是虛擬地址,進(jìn)程不允許直接訪(fǎng)問(wèn)物理地址,物理地址必須首先映射到虛擬地址上,不管是靜態(tài)映射還是動(dòng)態(tài)映射。 下圖是靜態(tài)映射的例子,CE 5.0版本和6.0版本映射圖是相同的。那么有個(gè)問(wèn)題:為什么要靜態(tài)映射??jì)H僅動(dòng)態(tài)映射不是很好嘛? 3.物理尋址在什么情況下需要物理尋址?跟Windows桌面系統一樣:在系統上電之后、MMU啟動(dòng)之前。另外Bus Mastering組件(比如DMA控制器)也會(huì )用到物理尋址。 虛擬內存總覽下圖是Windows CE 5.0的4GB虛擬內存分布圖,每個(gè)進(jìn)程單獨的內存是32MB,當前系統最多只允許存在32個(gè)進(jìn)程。 下圖是5.0各個(gè)進(jìn)程的內存發(fā)布以及具體一個(gè)進(jìn)程內部的內存發(fā)布,從右邊的圖可以看出,DLL存放的位置從高地址向下擴展,而EXE的位置從低地址向高地址擴展,當相遇時(shí)就會(huì )報地址不足錯誤。這也是5.0不足的地方。另外nk.exe比較特殊,總是運行在Slot 97位置。 下圖是Windows Embedded CE 6.0的4GB虛擬內存分布圖,每個(gè)進(jìn)程現在都有2GB的虛擬地址,拋棄了原來(lái)的按Slot分配的方式,而是只要你需要你就可以創(chuàng )建內存。關(guān)于更詳細的請見(jiàn)下面的內核態(tài)和用戶(hù)態(tài)地址空間。 內核態(tài)地址空間內核地址空間位于虛擬地址空間的較高的2GB,對于所有進(jìn)程在任何時(shí)候它都是存在的。相比來(lái)說(shuō)每個(gè)進(jìn)程所具有的較低的2GB用戶(hù)態(tài)地址空間相互之間是隔離的。 ▲CPU Specific VM ▲Kernel VM(if supported by CPU) 256 MB ▲Kernel VM 256 MB ▲Object Store 128 MB ▲Kernel XIP DLLs 128 MB ▲Static Mapped Uncached 512 MB ▲Static Mapped Cached 512 MB 用戶(hù)態(tài)地址空間▲Shared System Heap 255 MB ▲RAM Backed Mapfiles 256 MB ▲Shared User DLLs 512 MB ▲Process Space 1024 MB 程序的內存一個(gè)進(jìn)程至少有一個(gè)默認的堆,每個(gè)線(xiàn)程都有一個(gè)棧。 1.堆堆被作為應用程序主要的內存使用。堆分為以下幾種: ▲Local Heap (Default) Each application has a default, or local, heap created by the OS when an application launches. By default, Windows Embedded CE 6.0 initially reserves 64 KB of virtual memory for the local heap, but only commits the pages as they are allocated. If the application allocates more than the 64 KB in the local heap, the system allocates more virtual memory by using VirtualAlloc to fulfill the request for memory. (第一次系統默認給應用程序堆的大小事64K,當超過(guò)這個(gè)大小時(shí),系統使用VirtualAlloc分配更多的內存來(lái)滿(mǎn)足你的需求。) ▲Private Heap An application can also create any number of separate heaps. These private heaps have the same properties as the local heap but are managed through a separate set of heap functions. You can specify the maximum or initial size when creating private heaps.(這系列堆函數是HeapCreate, HeapAlloc, HeapFree, HeapReAlloc, HeapSize等。) ▲Shared Heap Shared heaps allow for efficient data transfer from kernel mode components to user processes. Shared heaps are writeable to kernel components, and read only to user processes. Shared heaps are located in a system wide area at the top of the user address space, and visible to all user processes. Therefore you should not put any sensitive data in a shared heap.(上圖用戶(hù)態(tài)地址空間中紅色部分即是,它對于內核是可寫(xiě)的,但對于用戶(hù)態(tài)進(jìn)程是只讀的。) ▲Remote Heap Remote heaps are a new feature of WindowsCE Embedded 6.0. A remote heap allows a server and client process to share memory safely. The server process creates and has full access to the remote heap, while the client process can read and optionally write to it. The client process can’t destroy the heap metadata.(為了更好的滿(mǎn)足客戶(hù)端/服務(wù)器端通信而產(chǎn)生的。) 2.棧Storage area for variables referenced in a program. 3.Heap Walker工具下圖是Windows Mobile 6.0 Prefessional模擬器的堆截圖: Each process has at least one heap, with the HF32_DEFAULT flag. This is the local heap that is created for every process. Notice some processes have more heaps (unnamed), these are private heaps that the process chose to create. Fixed means they are in use. Free means that they can be reused. BigBlock is a region of memory that was allocated outside the heap due to its size. Notice that BigBlock areas are created on 64KB boundaries, that is the granularity of the low level memory allocation APIs.(內存分配的粒度是64K,這是編程時(shí)應該注意的問(wèn)題,負責會(huì )造成內存浪費。) Target Control工具安裝了Platform Builder插件的Visual Studio 2005 SP1可以從Target->Target Control打開(kāi)Windows CE命令提示符窗口。在命令提示符后鍵入'mi’即可以看到內核和單獨進(jìn)程的內存信息。比如mi ["kernel","full"], kernel代表列出內核內存詳細信息,full代表列出全部?jì)却嫘畔ⅰ?/p> 下圖示出進(jìn)程HeapTest1.exe的內存信息,內存信息具體的含義見(jiàn)后面的解釋。 <blank> A blank space indicates a virtual page that is not currently allocated. Does not require a physical page. - Reserved but not in use. Indicates a virtual page that is currently allocated but not mapped to any physical memory. Does not require a physical page. C Code pages in ROM. Does not require a physical page. c Code pages in RAM. Requires a physical page. S Indicates a virtual page that holds a stack. Requires a physical page. P Peripheral memory (pages used to map target device memory by using VirtualAlloc). Indicates a virtual page that is used to map a range of hardware addresses. Does not require a physical page. Peripheral memory may include frame buffer memory. W Indicates a virtual page that holds read-write data. Requires a physical page. Read-write pages include global variables as well as dynamically allocated memory. O Indicates a virtual page that is used by the object store. Requires a physical page. Should only appear in the Filesys process. Contents unknown. r Read-only data pages in RAM. Requires a physical page. Read-only data primarily comes from data items that are declared as a const type in the source code. R Read-only data pages in ROM. Does not require a physical page. Read-only data primarily comes from data items that are declared as a const type in the source code. Note: For CPUs such as ARM and SHx that do not distinguish between read-only and executable code pages in hardware, use R(r) to represent both data and code. |
聯(lián)系客服