轉(zhuǎn)自:http://www.cnblogs.com/wangkewei/archive/2009/07/22/1528892.html 這篇文章是繼文章Windows Embedded CE 6.0 Internals (1)的。內(nèi)存這塊一直是讓人頭痛的東西,因為比較復(fù)雜,但是我們卻需要經(jīng)常與其打交道——內(nèi)存泄漏、異常定位、程序優(yōu)化等等。這篇文章以及后續(xù)的文章我試著能夠刨根問底。 5.內(nèi)存構(gòu)架內(nè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中的鏡像在執(zhí)行時被拷貝到RAM中。而NOR Flash是支持XIP的,這也是NAND和NOR的主要區(qū)別。) 虛擬內(nèi)存架構(gòu)1.虛擬內(nèi)存Windows Embedded CE 6.0使用單一的32位(4G)平坦模式虛擬內(nèi)存尋址空間。 這樣可以高效的、保護的使用物理內(nèi)存。 2.虛擬尋址有幾個概率需要澄清,首先是▲物理內(nèi)存是被內(nèi)存管理單元(MMU)擁有的,當然處理器得有MMU,不然沒法跑起來CE 6.0。 ▲把虛擬內(nèi)存轉(zhuǎn)換為物理內(nèi)存是MMU的工作。 ▲一個有效的虛擬內(nèi)存必須是已經(jīng)映射到物理內(nèi)存的。 ▲操作系統(tǒng)中近乎所有地址都是虛擬地址,進程不允許直接訪問物理地址,物理地址必須首先映射到虛擬地址上,不管是靜態(tài)映射還是動態(tài)映射。 下圖是靜態(tài)映射的例子,CE 5.0版本和6.0版本映射圖是相同的。那么有個問題:為什么要靜態(tài)映射?僅僅動態(tài)映射不是很好嘛? 3.物理尋址在什么情況下需要物理尋址?跟Windows桌面系統(tǒng)一樣:在系統(tǒng)上電之后、MMU啟動之前。另外Bus Mastering組件(比如DMA控制器)也會用到物理尋址。 虛擬內(nèi)存總覽下圖是Windows CE 5.0的4GB虛擬內(nèi)存分布圖,每個進程單獨的內(nèi)存是32MB,當前系統(tǒng)最多只允許存在32個進程。 下圖是5.0各個進程的內(nèi)存發(fā)布以及具體一個進程內(nèi)部的內(nèi)存發(fā)布,從右邊的圖可以看出,DLL存放的位置從高地址向下擴展,而EXE的位置從低地址向高地址擴展,當相遇時就會報地址不足錯誤。這也是5.0不足的地方。另外nk.exe比較特殊,總是運行在Slot 97位置。 下圖是Windows Embedded CE 6.0的4GB虛擬內(nèi)存分布圖,每個進程現(xiàn)在都有2GB的虛擬地址,拋棄了原來的按Slot分配的方式,而是只要你需要你就可以創(chuàng)建內(nèi)存。關(guān)于更詳細的請見下面的內(nèi)核態(tài)和用戶態(tài)地址空間。 內(nèi)核態(tài)地址空間內(nèi)核地址空間位于虛擬地址空間的較高的2GB,對于所有進程在任何時候它都是存在的。相比來說每個進程所具有的較低的2GB用戶態(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 用戶態(tài)地址空間▲Shared System Heap 255 MB ▲RAM Backed Mapfiles 256 MB ▲Shared User DLLs 512 MB ▲Process Space 1024 MB 程序的內(nèi)存一個進程至少有一個默認的堆,每個線程都有一個棧。 1.堆堆被作為應(yīng)用程序主要的內(nèi)存使用。堆分為以下幾種: ▲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. (第一次系統(tǒng)默認給應(yīng)用程序堆的大小事64K,當超過這個大小時,系統(tǒng)使用VirtualAlloc分配更多的內(nèi)存來滿足你的需求。) ▲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.(這系列堆函數(shù)是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.(上圖用戶態(tài)地址空間中紅色部分即是,它對于內(nèi)核是可寫的,但對于用戶態(tài)進程是只讀的。) ▲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.(為了更好的滿足客戶端/服務(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.(內(nèi)存分配的粒度是64K,這是編程時應(yīng)該注意的問題,負責會造成內(nèi)存浪費。) Target Control工具安裝了Platform Builder插件的Visual Studio 2005 SP1可以從Target->Target Control打開Windows CE命令提示符窗口。在命令提示符后鍵入'mi’即可以看到內(nèi)核和單獨進程的內(nèi)存信息。比如mi ["kernel","full"], kernel代表列出內(nèi)核內(nèi)存詳細信息,full代表列出全部內(nèi)存信息。 下圖示出進程HeapTest1.exe的內(nèi)存信息,內(nèi)存信息具體的含義見后面的解釋。 <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. |