Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Memory Management Douglas Boling President Boling Consulting Inc. www.bolingconsulting.com.

Similar presentations


Presentation on theme: "Advanced Memory Management Douglas Boling President Boling Consulting Inc. www.bolingconsulting.com."— Presentation transcript:

1 Advanced Memory Management Douglas Boling President Boling Consulting Inc. www.bolingconsulting.com

2

3 Speaker Douglas Boling dbolingmedc @ bolingconsulting.com Author – “Programming Microsoft Windows CE 3 rd Edition” Trainer – Classes on Windows CE App Development Windows CE OAL Development Consultant – Work with companies to help their application and platform development efforts

4 M anagement T ools C ommunications & M essaging Device Update Agent Software Update Services Live Communications Server Exchange Server Internet Security and Acceleration Server Speech Server Image Update L ocation S ervices M ultimedia MapPoint DirectX Windows Media Visual Studio 2005 D evelopment T ools MFC 8.0, ATL 8.0 Win32 N ative M anaged S erver S ide L ightweight R elational SQL Server 2005 Express EditionEDB D ata P rogramming M odel D evice B uilding T ools D evice B uilding T ools H ardware/ D rivers Windows XP DDK Windows Embedded Studio Platform Builder OEM/IHV Supplied BSP (ARM, SH4, MIPS) OEM Hardware and Standard Drivers Standard PC Hardware and Drivers SQL Server 2005SQL Server 2005 Mobile Edition ASP.NET Mobile ControlsASP.NET.NET Compact Framework.NET Framework Microsoft Operations Manager Systems Management Server

5 Agenda Slot Based Memory Model DLL Loading Issues Managed Applications

6 Virtual Address Space 0000 FFFF User Mode Space 8000 0000 Kernel Mode Space Accessible in user and kernel modes Accessible in kernel mode only

7 Windows CE Memory Architecture Windows CE uses a unified, 4 Gigabyte virtual address space The address space is divided into kernel and user mode spaces Kernel mode code has access to the entire address space User mode code can only access the lower 2 Gigabytes

8 Active Process Windows XP Memory Map System Reserved (kernel mode space) 0000 8000 0000 FFFF ApplicationSpace

9 Windows CE Memory Map System Reserved (kernel mode space) 0000 0400 0000 4200 0000 8000 0000 FFFF Active Process Application Space Reserved Large Memory Area (memory mapped files) Application Space

10 0000 0400 0000 4200 0000 8000 0000 Large Memory Area (memory mapped files) User Virtual Space Resource only DLLs (Slot 63) Application Slots (Slots 2-33) Current Application (Slots 0-1) 2 Gigabytes Divided into 64 32 MByte Slots 30 slots for LMA 31 slots for applications 2 slots for current application 1 resource slot

11 System Memory Map Memory space is divided into 33 slots One process per slot Slot 0 contains active process Remainder of memory shared or reserved

12 Application Memory Map COREDLL.DLL 0000 0001 0000 Read only data 03FF FFFF Code Read write data reserved Other ROM DLLs Resources Stack (reserved space) Heap (reserved space) Free virtual space Application Specific Space ROM DLL space 0200 0000 DLL Space (Constant for all applications) non-ROM DLLs

13 Application Memory Map Application space 64 Megabytes DLLs mapped into upper 32 Meg DLL static data mapped into lower 32M EXE code, heaps, stack, and RAM DLL use lower 32 Meg No way for an application to allocate memory above 32M

14 Problems with Memory Architecture 32 Meg isn’t that big Application code, data DLLs loaded by application DLLs must be loaded at a system wide unique address Windows CE loads all DLLs at a unique address across the entire system Gaps between XIP regions are not used by Windows CE for loading DLLs

15 Living in the Box Process limit of 32 Meg virtual space Is this a problem? Where? Virtual Allocations Large Allocations

16 VirtualAlloc The base memory allocation function is Parameters lpAddressCommit Address dwSizeSize of block flAllocationTypeMEM_RESERVE, MEM_COMMIT flProtect Protection flags LPVOID VirtualAlloc (LPVOID lpAddress, DWORD dwSize, DWORD flAllocationType, DWORD flProtect);

17 Virtual Memory Max 32 Meg address space per process You can get around this by using memory mapped objects Allocated on a page basis Virtual memory reserved on 64K boundaries Reserve large blocks then commit later

18 Limited Virtual Mem Space Applications need to consider their limited virtual memory NT applications have 2 Gigabytes CE applications have only 32 Megabytes Remember: virtual memory is reserved on 64K boundaries int i; PBYTE pMem[512]; // Allocate 512 pages with one call each allocation for (i = 0; i < 512; i++) { pMem[i] = (PBYTE)VirtualAlloc (0, PAGESIZE, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); }

19 Limited Virtual Mem Space Solutions: Reserve virtual memory in large amounts... …then commit later as needed Or, use Memory Mapped Objects They are not placed in the process’s slot int i; PBYTE pBase, pMem[512]; pBase = (PBYTE)VirtualAlloc (0, 512*PAGESIZE, MEM_RESERVE, PAGE_READWRITE); for (i = 0; i < 512; i++) { pMem[i] = (PBYTE)VirtualAlloc (pBase[PAGESIZE * i], PAGESIZE, MEM_COMMIT, PAGE_READWRITE); }

20 Large Virtual Allocs Windows CE supports requests for large (> 2Meg) VirtualAlloc calls Space is allocated outside of the 32M box Same “shared” space used by memory mapped objects Block is not protected from other apps Allocations must be reserved first then committed later

21 Large Virtual Allocs 0000 0400 0000 4200 0000 8000 0000 FFFF Active Process Application Space Large Memory Area Application Space Large VAllocs go here

22 DLL Load Positioning Windows CE has specific rules on how DLLs are positioned in memory These rules can cause problems for the unaware…

23 DLL Load Positioning 0000 DLLs Stack Heap DLL A Code DLLs Stack Heap DLL A DLL B DLL C Code DLLs Stack Heap DLL A DLL D DLL C Code

24 DLL Load Positioning DLL load address is dependant on all processes loaded in the system On open systems, you can’t predict if this problem will hit you DLLs are loaded on 64K boundaries The more DLLs the bigger the problem Misuse of multiple XIP Regions exacerbate the problem

25 Multiple XIP Regions OS DLLs Code Stack Heap First XIP Region XIP Region 2 XIP Region 3 Reserved Stack Heap DLL A Code First Load Address

26 Multiple XIP Regions The system reserves the space from the top of the first region to the bottom of the last Any gaps are not used to locate DLLs Windows Mobile systems before 5.0 use multiple XIP regions

27 DLL Problem Workarounds Fewer DLLs Every DLL takes up at least 64 Kbytes DLL size just under multiples of 64K It’s the regions that are important Move code from.DLL to.EXE Load DLLs in specific order Have large EXEs load their DLLs first

28 Windows Mobile 5.0 Updates no longer depend on multiple XIP regions This will solve the problem Check out the talks here at the DevCon

29 Kernel DLLs Kernel DLLs are quite powerful Inject a DLL into the kernel’s process space Have access to all sorts of cool information This is how lots of tools work CeLog Shim Engine Profiling (5.0) KCover

30 Kernel DLLs Don’t link to standard Win32 APIs The ‘reserved’ parameter in LibMain points to KernelLibIoControl BOOL KernelLibIoControl (HANDLE hModule, DWORD dwIoControlCode, LPVOID lpInBuf, DWORD nInBufSize, LPVOID lpOutBuf, DWORD nOutBufSize, LPDWORD lpBytesReturned );

31 Kernel DLLs All of this discussion is interesting, but almost all is unsupported. DON’T COMPLAIN if it doesn’t work Today, tomorrow, or ever!

32 Managed Code Managed Applications live within the slot Memory is allocated within the slot No matter how large the allocation

33 Managed Code Managed code loads DLLs on demand Native DLLs are treated as Win32 DLLs Managed DLLs are not Loaded as data by the runtime

34 Summary 32/64 MB Limit This problem has only been pushed out DLL Load Issue Problem in older devices Managed processes still have limits Memory allocated within slot

35 Questions dbolingmedc @ bolingconsulting.com

36 While at MEDC 2005… Fill out an evaluation for this session Randomly selected instant WIN prizes! Randomly selected instant WIN prizes! Use real technology in a lab Instructor led Reef E/F & Breakers L Self-paced Reef B/C Self-paced Reef B/C Visit the Microsoft Product Pavilion in the Exhibit Hall Shorelines B in the Exhibit Hall Shorelines B

37 After The Conference… Develop Build InstallBuildJoin Install Enter Enter Join Full-featured trial versions of Windows CE and/or Windows XP Embedded Cool stuff & tell us about it: msdn.microsoft.com/embedded/community msdn.microsoft.com/embedded/community Windows Embedded Partner Program: www.mswep.com www.mswep.com Windows Mobile 5.0 Eval Kit including Visual Studio 2005 Beta 2 Mobile2Market Contest and win up to $25000: mobile2marketcontest.com mobile2marketcontest.com Microsoft Solutions Partner Program: partner.microsoft.com partner.microsoft.com

38 Tools & Resources msdn.microsoft.com/ embedded microsoft.public. windowsxp.embedded windowsce.platbuilder windowsce.platbuilder windowsce.embedded.vc windowsce.embedded.vc blogs.msdn.com/ mikehall Windows CE 5.0 Eval Kit Windows XP Embedded Eval Kit msdn.microsoft.com/ mobility microsoft.public. pocketpc.developer smartphone.developer dotnet.framework.compactframework blogs.msdn.com/ windowsmobile vsdteam netcfteam Windows Mobile 5.0 Eval Kit Websites Newsgroups Blogs Tools Build Develop

39 © 2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.


Download ppt "Advanced Memory Management Douglas Boling President Boling Consulting Inc. www.bolingconsulting.com."

Similar presentations


Ads by Google