94% of apps peak at
TRANSCRIPT
Andrew WhitechapelProgram ManagerMemory and CPU Policy in Univeral Apps
Expand Your Reach by Tuning for Memory and CPU Policies
3-91
Why do I care?What does policy cover?Why does the system do this?How does policy vary per SKU or device?Show me the code
Agenda slide
• I can make my app scale across multiple devices and SKUs
• I can make my app resilient to ongoing conditions• Hardware, connectivity, state changes
• My app doesn’t show up on the battery wall of shame
• My app doesn’t crash or cause the device to become unusable
• I can use a rich set of features without degrading the system
Why do I care?
• Resources are finite• Maintaining an optimal balance• Different strokes for different task types• Mapping scenario priority to resource
sets• Achieving predictability• Allow apps to understand and
cooperate in the running ecosystem• Adjusting dynamically
Why does the system do this?
• Policy is holistic• Memory• CPU and task lifetime• User expectations and scenario
prioritization• Gated access to sensors• Other constraints
What does policy cover?
Memory breakdown examples 94% of apps
peak at <150MB.
92% of apps average at <100MB.
Component usage (ballpark)512MB Phone
1GB Tablet
2GB Desktop
Total physical 512 1024 2048
Paging level 200 200 1600
712 1224 3648
BSP, OS, drivers, services 425 425 425
OEM/MO service agents 22 40
VOIP or Background audio 66 80 80
VPN 10 10 20
523 555 525
Foreground App 185 390 900Back-stack, Task completion, BG tasks, More FG apps 4 279 2223
Different strokes
Task typeMemory Cap MB
Lifetime CPU
Block in Batt-Sav
Default Background Task 16 30 sec 10% Y
Long-Running Bluetooth 16 ∞ 10% Y
Extended Execution 150 10 min 10% YBackground Audio Playback 25 ∞ 10% N
VOIP Active Call 66 ∞ 70% N
Modern Foreground App 185 ∞ 70% N
Example: 512MB
device
• Scenario-based prioritization.
Task Priorities
*Higher priority if part of the Foreground experience.
Task type Priority
Foreground App 2
VOIP 2
Background Audio 2
VPN 2
Background Transfer 2 or 6*
Bluetooth 3CachedFileUpdater/ShareDataPackage Task 3 or 6*
Task Completion 4
Geofence-triggered Task 5
Default Background Tasks 6
App in the backstack 7
Example: Background Audio Agent
Sensor resources
Resource Allowed?
Microphone Camera Proximity Gyro Compass Accelerometer Vibrator Push SpeechSynthesis SpeechRecognition Audio InterruptiveUI
• Protected sensor resources• MemoryManager API• EnergyManager API
Show me the code
Sensor Resource APIsNetworkInformation.NetworkStatusChanged += OnNetworkStatusChanged;Battery.AggregateBattery.ReportUpdated += OnBatteryReportUpdated; ;
bool isGyro = Gyrometer.GetDefault() != null;bool isAccelero = Accelerometer.GetDefault() != null;bool isCompass = Compass.GetDefault() != null;MediaCapture mediaCapture = new MediaCapture();bool isCamera = mediaCapture != null;
BatteryReport batteryReport = Battery.AggregateBattery.GetReport();if (batteryReport.Status != BatteryStatus.NotPresent){ int charge = (int)batteryReport.RemainingCapacityInMilliwattHours;}
ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();ConnectionCost cost = profile.GetConnectionCost();if (cost.Roaming || cost.OverDataLimit) ; // prompt for user opt-in.else if (cost.ApproachingDataLimit) ; // throttle behavior.
MemoryManager APIsMemoryManager.AppMemoryUsageIncreased += OnAppMemoryUsageIncreased;MemoryManager.AppMemoryUsageDecreased += OnAppMemoryUsageDecreased;
ulong usageLimit = MemoryManager.AppMemoryUsageLimit;ulong usage = MemoryManager.AppMemoryUsage;ulong headroom = usageLimit - usage;
AppMemoryReport appReport = MemoryManager.GetAppMemoryReport();ulong privateCommit = appReport.PrivateCommitUsage;ulong peakPrivate = appReport.PeakPrivateCommitUsage;ulong totalCommit = appReport.TotalCommitUsage;ulong commitLimit = appReport.TotalCommitLimit;
ProcessMemoryReport processReport = MemoryManager.GetProcessMemoryReport();ulong privateSet = processReport.PrivateWorkingSetUsage;ulong totalSet = processReport.TotalWorkingSetUsage;
EnergyManager APIsBackgroundEnergyManager.RecentEnergyUsageIncreased += OnRecentEnergyUsageIncreased;BackgroundEnergyManager.RecentEnergyUsageReturnedToLow += OnRecentEnergyUsageReturnedToLow;
uint usage = BackgroundEnergyManager.RecentEnergyUsage;
uint usageLevel = BackgroundEnergyManager.RecentEnergyUsageLevel;if (usageLevel == BackgroundEnergyManager.NearTerminationUsageLevel) ;// Get ready to be terminated.else if (usageLevel == BackgroundEnergyManager.ExcessiveUsageLevel) ;// Throttle back urgently.else if (usageLevel == BackgroundEnergyManager.MaxAcceptableUsageLevel) ;// On the wall of shame.else if (usageLevel == BackgroundEnergyManager.NearMaxAcceptableUsageLevel) ;// Approaching the wall of shame.else if (usageLevel == BackgroundEnergyManager.LowUsageLevel) ;// Good citizen.
• Variation by SKU• Mobile vs Desktop vs HoloLens vs IoT vs Xbox
• Variation by device• Physical RAM, Pagefile, Screen resolution
• Variation by app technology• Universal apps, pre-Windows 10 Phone, pre-Windows
10 Modern Desktop, Win32
How does policy vary?
• Target a wide range of SKUs and devices
• Query the system to discover your constraints
• Listen for change events• Tune your behavior accordingly
What’s the moral of the story?
© 2015 Microsoft Corporation. All rights reserved.