How to get current memory usage by OS/device in a UWP Windows10 app programmatically?

ANURAG24 0 Reputation points
2024-05-02T14:09:20.57+00:00

Hi Everyone,

I am working on a Windows 10 UWP app and I am trying to get to current memory usage by OS in an app like we see the memory in task manager. I am unable to find a solution.

Did anyone come across such a requirement or worked on it? Please help with a code snippet or any links with a solution.

Device memory

Thanks in advance!

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,068 questions
Universal Windows Platform (UWP)
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Roy Li - MSFT 32,721 Reputation points Microsoft Vendor
    2024-05-03T02:25:21.74+00:00

    Hello,

    Welcome to Microsoft Q&A!

    How to get current memory usage by OS/device in a UWP Windows10 app programmatically?

    Currently, there is no API in UWP that could get the memory usage of the system. .NET APIs like PerformanceCounter are not support in UWP.

    You might need to take a look at GlobalMemoryStatusEx function which is a Win32 API that could be used in UWP. Based on the document, this API should be able to get the memory usage of the system that you required. Since it's a Win32 API, there is no code sample about how to use it in UWP app. You need to do it by yourself.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. ANURAG24 0 Reputation points
    2024-05-03T08:58:28.91+00:00

    using Windows.System.Diagnostics;

    ulong availableSizeInBytes = SystemDiagnosticInfo.GetForCurrentSystem().MemoryUsage.GetReport().AvailableSizeInBytes;

    ulong totalPhysicalSizeInBytes = SystemDiagnosticInfo.GetForCurrentSystem().MemoryUsage.GetReport().TotalPhysicalSizeInBytes;

    decimal percentFree = ((decimal)availableSizeInBytes / (decimal)totalPhysicalSizeInBytes) * 100;

    decimal percentOccupied = 100 - percentFree;

    I can achieve it with the above code. Is it the correct way?