Advanced Memory Management Douglas Boling President Boling Consulting Inc. .

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

description

Speaker Douglas Boling 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

Transcript of Advanced Memory Management Douglas Boling President Boling Consulting Inc. .

Page 1: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

Advanced Memory Advanced Memory ManagementManagementDouglas BolingDouglas BolingPresidentPresidentBoling Consulting Inc.Boling Consulting Inc.www.bolingconsulting.comwww.bolingconsulting.com

Page 2: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .
Page 3: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 4: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 5: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

AgendaAgendaSlot Based Memory ModelSlot Based Memory Model

DLL Loading IssuesDLL Loading Issues

Managed ApplicationsManaged Applications

Page 6: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 7: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 8: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 9: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 10: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 11: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 12: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 13: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 14: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 15: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 16: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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);

Page 17: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 18: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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);}

Page 19: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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);}

Page 20: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 21: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 22: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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…

Page 23: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 24: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 25: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 26: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 27: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 28: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 29: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 30: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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 );

Page 31: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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!

Page 32: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 33: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 34: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 35: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

QuestionsQuestions

dbolingmedc @ bolingconsulting.comdbolingmedc @ bolingconsulting.com

Page 36: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 37: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 38: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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

Page 39: Advanced Memory Management Douglas Boling President Boling Consulting Inc.  .

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