这篇文章是继文章Windows Embedded CE 6.0 Internals (1)的。内存这块一直是让人头痛的东西，因为比较复杂，但是我们却需要经常与其打交道——内存泄漏、异常定位、程序优化等等。这篇文章以及后续的文章我试着能够刨根问底。
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).
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中的镜像在执行时被拷贝到RAM中。而NOR Flash是支持XIP的，这也是NAND和NOR的主要区别。)
Windows Embedded CE 6.0使用单一的32位(4G)平坦模式虚拟内存寻址空间。
下图是Windows CE 5.0的4GB虚拟内存分布图，每个进程单独的内存是32MB，当前系统最多只允许存在32个进程。
下图是Windows Embedded CE 6.0的4GB虚拟内存分布图，每个进程现在都有2GB的虚拟地址，抛弃了原来的按Slot分配的方式，而是只要你需要你就可以创建内存。关于更详细的请见下面的内核态和用户态地址空间。
▲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
▲Shared System Heap 255 MB
▲RAM Backed Mapfiles 256 MB
RAM backed map files are memory mapped file objects that do not have an actual file underneath them. This region provides backward compatibility for applications that used RAM-backed map files for cross-process communication, expecting all processes to map views at the same virtual address. Every process that opens the same RAM backed memory mapped file will get the same pointer value. File backed memory mapped files will be allocated from the virtual memory area of the process, and differ for each process.
▲Shared User DLLs 512 MB
When a process initializes, the OS maps the following DLLs and memory components: DLLs and ROM DLL read/write sections are loaded bottom up starting at 1GB. DLLs are controlled by the loader, which loads all the DLLs at the same address for each process.
▲Process Space 1024 MB
The stack, the heap, and the executable (.exe) file are created and mapped from the bottom up starting at 64KB. Virtual memory allocations occur in the first 1GB after the executable code and data. The bottom 64 KB of memory always remains free.
▲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，当超过这个大小时，系统使用VirtualAlloc分配更多的内存来满足你的需求。)
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 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.(上图用户态地址空间中红色部分即是，它对于内核是可写的，但对于用户态进程是只读的。)
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.(为了更好的满足客户端/服务器端通信而产生的。)
Storage area for variables referenced in a program.
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，这是编程时应该注意的问题，负责会造成内存浪费。)
安装了Platform Builder插件的Visual Studio 2005 SP1可以从Target->Target Control打开Windows CE命令提示符窗口。在命令提示符后键入'mi’即可以看到内核和单独进程的内存信息。比如mi ["kernel","full"], kernel代表列出内核内存详细信息，full代表列出全部内存信息。
<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.