Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 436
Adrian K Avatar asked Jun 16 '16 05:06

Adrian K


People also ask

What Windows app can you use to check the memory usage of each running app?

Check Computer Memory Usage Easily To open up Resource Monitor, press Windows Key + R and type resmon into the search box. Resource Monitor will tell you exactly how much RAM is being used, what is using it, and allow you to sort the list of apps using it by several different categories.

How do I reduce memory usage in WPF?

Try this: Open the task manager, go to the processes tab, go to view, select collums and check the box that says VM size. Notice that when you minimize or restore a program or application the memory changes from what's in Memory Usage to VM size.


2 Answers

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();
like image 142
Romasz Avatar answered Sep 25 '22 04:09

Romasz


For UWP apps there is ProcessDiagnosticInfo class available.

ProcessMemoryUsageReport mu = ProcessDiagnosticInfo.GetForCurrentProcess().MemoryUsage.GetReport();
like image 32
Alexej Sommer Avatar answered Sep 24 '22 04:09

Alexej Sommer