How to see (approximate) memory usage in an UWP App while it's running

拥有回忆 提交于 2019-12-06 02:20:35

问题


According to this article on Diagnosing memory issues with the new Memory Usage Tool in Visual Studio:

1. Memory caps on Phone devices: In particular for Phone, there are specific memory limits enforced on an application based on the size of the memory in the device. Allocating more than the specified limit will cause an OutOfMemoryException and will result in app termination.

All well and good, and in Visual Studio you can use the Diagnostics tool to see memory usage during development.

Is there any way a UWP app running on a (Windows 10) phone can get an approximate idea of how much memory it's consuming? - I.e. within the app, not by leveraging visual studio.

Update - How I chose the 'Answer'

The key thing is that this has exposed a massive lack of understanding on my part regarding memory in general, and specifically how modern .net apps consume it. Both of these answers were of help to me, and even though I have experimented briefly with both its hard to say either one of them is the right answer as I'm finding both of them useful.

Also whilst I appreciate both the answers linking to the appropriate official documentation, that information is pretty thin (no disrespect to Romasz & Alexej).

In the end I've awarded the Answer to Romasz as the API seems a little deeper.


回答1:


I think you can also make use of MemoryManager class. There you can subscribe to events informing about increase/decrease of memory, set limits, as well as check memory usage of app or read reports for app or for process:

var appMemory = MemoryManager.AppMemoryUsage;
var appMemoryReport = MemoryManager.GetAppMemoryReport();
var processMemoryReport = MemoryManager.GetProcessMemoryReport();



回答2:


For UWP apps there is ProcessDiagnosticInfo class available.

ProcessMemoryUsageReport mu = ProcessDiagnosticInfo.GetForCurrentProcess().MemoryUsage.GetReport();


来源:https://stackoverflow.com/questions/37850354/how-to-see-approximate-memory-usage-in-an-uwp-app-while-its-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!