UWP 设备总内存

UWP device total memory

如何确定设备的总内存?我想在低内存设备上使用顺序程序流,在高内存设备上使用更异步的程序流。

示例:在具有 1GB 内存的设备上,我的程序可以运行,但在 512MB 的设备上,我的程序会遇到 OutOfMemoryException,因为它正在异步缓存来自多个站点的图像。

MemoryManager class 有一些静态属性来获取应用程序的当前使用情况和限制。

// 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

您可以使用 MemoryManager.AppMemoryUsageLimitChanging 事件

对限制更改做出反应
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));
}

您可以使用应用程序的内存限制来决定如何最好地管理您的内存分配。