Advanced Memory Advanced Memory Management Management Douglas Boling Douglas Boling President President Boling Consulting Inc. Boling Consulting Inc. www.bolingconsulting.com www.bolingconsulting.com
Jan 18, 2018
Advanced Memory Advanced Memory ManagementManagementDouglas BolingDouglas BolingPresidentPresidentBoling Consulting Inc.Boling Consulting Inc.www.bolingconsulting.comwww.bolingconsulting.com
SpeakerSpeakerDouglas Boling Douglas Boling dbolingmedc @ bolingconsulting.comdbolingmedc @ bolingconsulting.com
Author – “Programming MicrosoftAuthor – “Programming MicrosoftWindows CE 3Windows CE 3rdrd Edition” Edition”
Trainer – Classes on Trainer – Classes on Windows CE App DevelopmentWindows CE App Development
Windows CE OAL Windows CE OAL Development Development Consultant – Work with companies to Consultant – Work with companies to
help their help their application and application and platform platform development effortsdevelopment efforts
MManagementanagementTToolsools
CCommunicationsommunications& & MMessagingessaging
Device Update AgentSoftware Update Services
Live Communications ServerExchange Server
Internet Security and Acceleration Server
Speech Server
Image Update
LLocation ocation SServiceservices
MMultimediaultimedia
MapPoint
DirectXWindows Media
Visual Studio 2005DDevelopment evelopment TToolsools
MFC 8.0, ATL 8.0Win32NNativeative
MManagedanagedSServer erver SSideide
LLightweightightweightRRelationalelational
SQL Server 2005 Express EditionEDB
DDat
aat
aPP r
ogra
mm
ing
rogr
amm
ing
MMod
elod
el
DDevice evice BBuilding uilding TToolsools
HHardware/ardware/DDriversrivers
Windows XP DDKWindows Embedded
StudioPlatform 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 Controls ASP.NET.NET Compact Framework .NET Framework
Microsoft Operations ManagerSystems Management Server
AgendaAgendaSlot Based Memory ModelSlot Based Memory Model
DLL Loading IssuesDLL Loading Issues
Managed ApplicationsManaged Applications
Virtual Address SpaceVirtual Address Space
0000 0000
FFFF FFFF
User ModeSpace
8000 0000
Kernel ModeSpace
Accessible in user and kernel modes
Accessible in kernel mode only
Windows CE Memory Windows CE Memory ArchitectureArchitecture
Windows CE uses a unified, 4 Gigabyte Windows CE uses a unified, 4 Gigabyte virtual address spacevirtual address space
The address space is divided into The address space is divided into kernel and user mode spaceskernel and user mode spaces
Kernel mode code has access to the Kernel mode code has access to the entire address spaceentire address spaceUser mode code can only access the User mode code can only access the lower 2 Gigabyteslower 2 Gigabytes
Active ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive Process
Windows XP Memory MapWindows XP Memory Map
System ReservedSystem Reserved(kernel mode space)(kernel mode space)
0000 0000
8000 0000
FFFF FFFF
ApplicationApplicationSpaceSpace
Windows CE Memory MapWindows CE Memory Map
System ReservedSystem Reserved(kernel mode space)(kernel mode space)
0000 0000
0400 0000
4200 0000
8000 0000
FFFF FFFF
Active ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessApplication Space
ReservedReserved
Large Memory AreaLarge Memory Area(memory mapped files)(memory mapped files)
Application SpaceApplication Space
0000 00000400 0000
4200 0000
8000 0000
Large Memory Area(memory mapped files)
User Virtual SpaceUser Virtual SpaceResource only DLLs(Slot 63)
Application Slots
(Slots 2-33)
Current Application(Slots 0-1)
2 Gigabytes2 Gigabytes
Divided into 64 32 Divided into 64 32 MByte SlotsMByte Slots
30 slots for LMA30 slots for LMA
31 slots for 31 slots for applicationsapplications
2 slots for current 2 slots for current applicationapplication
1 resource slot 1 resource slot
System Memory MapSystem Memory MapMemory space is divided into 33 slotsMemory space is divided into 33 slots
One process per slotOne process per slot
Slot 0 contains active processSlot 0 contains active process
Remainder of memory shared or Remainder of memory shared or reservedreserved
Application Memory MapApplication Memory MapCOREDLL.DLL
0000 00000001 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
Application Memory MapApplication Memory MapApplication space 64 MegabytesApplication space 64 Megabytes
DLLs mapped into upper 32 MegDLLs mapped into upper 32 MegDLL static data mapped into lower 32MDLL static data mapped into lower 32M
EXE code, heaps, stack, and RAM DLL EXE code, heaps, stack, and RAM DLL use lower 32 Meguse lower 32 Meg
No way for an application to allocate No way for an application to allocate memory above 32Mmemory above 32M
Problems with Memory Problems with Memory ArchitectureArchitecture
32 Meg isn’t that big32 Meg isn’t that bigApplication code, dataApplication code, dataDLLs loaded by applicationDLLs loaded by application
DLLs must be loaded at a system wide DLLs must be loaded at a system wide unique addressunique address
Windows CE loads all DLLs at a unique Windows CE loads all DLLs at a unique address across the entire systemaddress across the entire system
Gaps between XIP regions are not used Gaps between XIP regions are not used by Windows CE for loading DLLsby Windows CE for loading DLLs
Living in the BoxLiving in the BoxProcess limit of 32 Meg virtual spaceProcess limit of 32 Meg virtual space
Is this a problem?Is this a problem?
Where?Where?Virtual AllocationsVirtual AllocationsLarge AllocationsLarge Allocations
VirtualAllocVirtualAllocThe base memory allocation function isThe base memory allocation function is
ParametersParameterslpAddresslpAddress Commit AddressCommit AddressdwSizedwSize Size of blockSize of blockflAllocationTypeflAllocationType MEM_RESERVE, MEM_RESERVE,
MEM_COMMITMEM_COMMITflProtect flProtect Protection flagsProtection flags
LPVOID VirtualAlloc (LPVOID lpAddress, DWORD dwSize, DWORD flAllocationType, DWORD flProtect);
Virtual MemoryVirtual MemoryMax 32 Meg address space per processMax 32 Meg address space per process
You can get around this by using memory mapped objectsYou can get around this by using memory mapped objects
Allocated on a page basisAllocated on a page basis
Virtual memory reserved on 64K boundariesVirtual memory reserved on 64K boundariesReserve large blocks then commit laterReserve large blocks then commit later
Limited Virtual Mem SpaceLimited Virtual Mem SpaceApplications need to consider their Applications need to consider their limited virtual memorylimited virtual memory
NT applications have 2 GigabytesNT applications have 2 GigabytesCE applications have only 32 MegabytesCE applications have only 32 Megabytes
Remember: virtual memory is reserved Remember: virtual memory is reserved on on 64K64K boundaries boundaries
int i;PBYTE pMem[512];// Allocate 512 pages with one call each allocationfor (i = 0; i < 512; i++) {
pMem[i] = (PBYTE)VirtualAlloc (0, PAGESIZE, MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE);}
Limited Virtual Mem SpaceLimited Virtual Mem SpaceSolutions:Solutions:
Reserve virtual memory in large amounts...Reserve virtual memory in large amounts...……then commit later as neededthen commit later as needed
Or, use Memory Mapped ObjectsOr, use Memory Mapped ObjectsThey are not placed in the process’s slotThey 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);}
Large Virtual AllocsLarge Virtual AllocsWindows CE supports requests for Windows CE supports requests for large (> 2Meg) VirtualAlloc callslarge (> 2Meg) VirtualAlloc calls
Space is allocated outside of the 32M boxSpace is allocated outside of the 32M boxSame “shared” space used by memory mapped Same “shared” space used by memory mapped objectsobjects
Block is Block is notnot protected from other apps protected from other apps
Allocations must be reserved first then Allocations must be reserved first then committed latercommitted later
Large Virtual AllocsLarge Virtual Allocs
0000 0000
0400 0000
4200 0000
8000 0000
FFFF FFFF
Active ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessActive ProcessApplication Space
Large Memory AreaLarge Memory Area
Application SpaceApplication Space
Large VAllocs go here
DLL Load PositioningDLL Load PositioningWindows CE has specific rules on how Windows CE has specific rules on how DLLs are positioned in memoryDLLs are positioned in memory
These rules can cause problems for the These rules can cause problems for the unaware…unaware…
DLL Load PositioningDLL Load Positioning
0000 0000
DLLs
Stack
Heap
DLL ADLL A
Code
DLLs
Stack
Heap
DLL ADLL ADLL BDLL B
DLLDLL CC
Code
DLLs
Stack
Heap
DLL ADLL A
DLL DDLL D
DLL CDLL C
Code
DLL Load PositioningDLL Load PositioningDLL load address is dependant on all DLL load address is dependant on all processes loaded in the systemprocesses loaded in the system
On open systems, you can’t predict if this On open systems, you can’t predict if this problem will hit youproblem will hit you
DLLs are loaded on 64K boundariesDLLs are loaded on 64K boundariesThe more DLLs the bigger the problemThe more DLLs the bigger the problem
Misuse of multiple XIP Regions Misuse of multiple XIP Regions exacerbate the problem exacerbate the problem
Multiple XIP RegionsMultiple XIP RegionsOS DLLs
Code
Stack
Heap
First XIP Region
XIP Region 2
XIP Region 3
Reserved
Stack
Heap
DLL ADLL A
Code
First Load Address
Multiple XIP RegionsMultiple XIP RegionsThe system reserves the space from the The system reserves the space from the top of the first region to the bottom of top of the first region to the bottom of the lastthe last
Any gaps are not used to locate DLLsAny gaps are not used to locate DLLs
Windows Mobile systems before 5.0 use Windows Mobile systems before 5.0 use multiple XIP regionsmultiple XIP regions
DLL Problem WorkaroundsDLL Problem WorkaroundsFewer DLLsFewer DLLs
Every DLL takes up at least 64 KbytesEvery DLL takes up at least 64 Kbytes
DLL size just under multiples of 64KDLL size just under multiples of 64KIt’s the regions that are importantIt’s the regions that are important
Move code from .DLL to .EXEMove code from .DLL to .EXE
Load DLLs in specific orderLoad DLLs in specific orderHave large EXEs load their DLLs firstHave large EXEs load their DLLs first
Windows Mobile 5.0Windows Mobile 5.0Updates no longer depend on multiple Updates no longer depend on multiple XIP regionsXIP regions
This will solve the problemThis will solve the problem
Check out the talks here at the DevConCheck out the talks here at the DevCon
Kernel DLLs Kernel DLLs Kernel DLLs are quite powerfulKernel DLLs are quite powerful
Inject a DLL into the kernel’s process Inject a DLL into the kernel’s process spacespaceHave access to all sorts of cool Have access to all sorts of cool informationinformationThis is how lots of tools workThis is how lots of tools work
CeLogCeLogShim EngineShim EngineProfiling (5.0)Profiling (5.0)KCoverKCover
Kernel DLLsKernel DLLsDon’t link to standard Win32 APIsDon’t link to standard Win32 APIs
The ‘reserved’ parameter in LibMain The ‘reserved’ parameter in LibMain points to KernelLibIoControlpoints to KernelLibIoControl
BOOL KernelLibIoControl (HANDLE hModule, DWORD dwIoControlCode, LPVOID lpInBuf, DWORD nInBufSize, LPVOID lpOutBuf, DWORD nOutBufSize, LPDWORD lpBytesReturned );
Kernel DLLsKernel DLLsAll of this discussion is interesting, but All of this discussion is interesting, but almost all is unsupported.almost all is unsupported.
DON’T COMPLAIN if it doesn’t workDON’T COMPLAIN if it doesn’t workToday, tomorrow, or ever!Today, tomorrow, or ever!
Managed CodeManaged CodeManaged Applications live within the Managed Applications live within the slotslot
Memory is allocated within the slotMemory is allocated within the slotNo matter how large the allocationNo matter how large the allocation
Managed CodeManaged CodeManaged code loads DLLs on demandManaged code loads DLLs on demand
Native DLLs are treated as Win32 DLLsNative DLLs are treated as Win32 DLLs
Managed DLLs are notManaged DLLs are notLoaded as data by the runtimeLoaded as data by the runtime
SummarySummary32/64 MB Limit32/64 MB Limit
This problem has only been pushed outThis problem has only been pushed out
DLL Load IssueDLL Load IssueProblem in older devicesProblem in older devices
Managed processes still have limitsManaged processes still have limitsMemory allocated within slotMemory allocated within slot
QuestionsQuestions
dbolingmedc @ bolingconsulting.comdbolingmedc @ bolingconsulting.com
While at MEDC 2005…While at MEDC 2005…Fill outFill out an evaluation for this session an evaluation for this session
Randomly selected instant Randomly selected instant WINWIN prizes! prizes!
Use Use real technology in a labreal technology in a lab Instructor led Instructor led Reef E/FReef E/F & & Breakers LBreakers L Self-paced Self-paced Reef B/CReef B/C
VisitVisit the Microsoft Product Pavilion the Microsoft Product Pavilion in the Exhibit Hall in the Exhibit Hall Shorelines BShorelines B
After The Conference…After The Conference…
DevelopDevelop
BuildBuildInstallInstall
BuildBuild
JoinJoin
InstallInstall
EnterEnter JoinJoin
Full-featured trial versions of Windows CE Full-featured trial versions of Windows CE and/or Windows XP Embeddedand/or Windows XP EmbeddedCool stuff & tell us about it: Cool stuff & tell us about it: msdn.microsoft.com/embedded/community Windows Embedded Partner Program:Windows Embedded Partner Program:www.mswep.com
Windows Mobile 5.0 Eval Kit including Windows Mobile 5.0 Eval Kit including Visual Studio 2005 Beta 2Visual Studio 2005 Beta 2Mobile2Market Contest and win up to $25000: Mobile2Market Contest and win up to $25000: mobile2marketcontest.com Microsoft Solutions Partner Program:Microsoft Solutions Partner Program:partner.microsoft.com
Tools & ResourcesTools & Resources
msdn.microsoft.com/msdn.microsoft.com/ embeddedembedded
microsoft.public.microsoft.public. windowsxp.embeddedwindowsxp.embedded windowsce.platbuilderwindowsce.platbuilder windowsce.embedded.vcwindowsce.embedded.vc
blogs.msdn.com/blogs.msdn.com/ mikehallmikehall
Windows CE 5.0 Eval KitWindows CE 5.0 Eval KitWindows XP Embedded Eval KitWindows XP Embedded Eval Kit
msdn.microsoft.com/msdn.microsoft.com/ mobilitymobility
microsoft.public.microsoft.public. pocketpc.developer pocketpc.developer smartphone.developer smartphone.developer dotnet.framework.compactframeworkdotnet.framework.compactframework
blogs.msdn.com/blogs.msdn.com/ windowsmobilewindowsmobile vsdteamvsdteam netcfteamnetcfteam
Windows Mobile 5.0 Eval KitWindows Mobile 5.0 Eval Kit
WebsitesWebsites
NewsgroupsNewsgroups
BlogsBlogs
ToolsTools
BuildBuild DevelopDevelop
© 2005 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.