Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP device total memory

How do you determine a devices total memory? I would like to use a sequential program flow on low memory devices and a more asynchronous flow on higher memory devices.

Example: On a device with 1GB of memory my program works but on 512MB device my program hits an OutOfMemoryException as it is caching images from multiple sites asynchronously.

like image 733
David Schmidlin Avatar asked Apr 24 '16 16:04

David Schmidlin


1 Answers

The MemoryManager class has some static properties to get the current usage and limit for the application.

// Gets the app's current memory usage.
MemoryManager.AppMemoryUsage

// Gets the app's memory usage level.
MemoryManager.AppMemoryUsageLevel

// Gets the app's memory usage limit.
MemoryManager.AppMemoryUsageLimit

You can react to the limit changing using the MemoryManager.AppMemoryUsageLimitChanging event

private void OnAppMemoryUsageLimitChanging(
    object sender, AppMemoryUsageLimitChangingEventArgs e)
{
    Debug.WriteLine(String.Format("AppMemoryUsageLimitChanging: old={0} MB, new={1} MB", 
        (double)e.OldLimit / 1024 / 1024,
        (double)e.NewLimit / 1024 / 1024));
}

You can use the application's memory limit to decide how best to manage your memory allocation.

like image 120
Glen Thomas Avatar answered Oct 30 '22 00:10

Glen Thomas