Ram consuption above Threshold

Dani_S 4,091 Reputation points
2025-02-12T10:20:18.47+00:00

Hi,

In maui version 8 there is a problem when moving pages the memory not release.

I would like to ask how to observe the RAM and if it pass threshold x it will be presanted

in UI label in red color, otherwise in green color.

Use MVVM approach.

Thanks,

Update


You can also add message to end user that block the UI that RAM consuption pass threshold.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,942 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 46,036 Reputation points
    2025-02-14T07:50:07.2433333+00:00

    Can you please give a full sample ?

    It is already a full sample; it is really just one line of code.

    Addtional see https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.privatememorysize64?view=net-9.0


  2. Dani_S 4,091 Reputation points
    2025-02-18T11:39:15.25+00:00

    System.Diagnostics.PerformanceCounter _ramCounter = new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes");

    var ramCapacity = GetTotalMemoryInGb();

    var ramUsageInPercenatages = ((ramCapacity - (_ramCounter.NextValue() / (1024D)))/ramCapacity)*100;

    private static ulong GetTotalMemoryInGb()
    
    {
    
        ulong capacityGB = 0;
    
        try
    
        {
    
            var query = "SELECT Capacity FROM Win32_PhysicalMemory";
    
            var searcher = new ManagementObjectSearcher(query);
    
            foreach (var WniPART in searcher.Get())
    
            {
    
                var capacity = Convert.ToUInt64(WniPART.Properties["Capacity"].Value);
    
                var capacityKB = capacity / 1024;
    
                var capacityMB = capacityKB / 1024;
    
                capacityGB = capacityMB / 1024;
    
            }
    
            return capacityGB;
    
        }
    
        catch (Exception ex)
    
        {
    
            _logger.Error("GetTotalMemoryInGb", ex);
    
            return capacityGB;
    
        }
    
    }
    
        
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.