none
How to detect "OutOfMemory" warning in UWP app RRS feed

  • Question

  • Hi 

    I would like to detect system out of memory warning in UWP app, before it throws any error.

    Usually app will crash for this out of memory exception,

    I would like to detect it earlier, so that I can save data which i keep in static local memory.

    Kindly help.

    Thanks in advance.

    Noorul 


    Noorul.

    Thursday, December 19, 2019 6:11 AM

Answers

  • You need to judge whether AppMemoryUsageLevel is OverLimit or High in event.

    like the sample in the document:

    private void MemoryManager_AppMemoryUsageIncreased(object sender, object e)
    {
        // Obtain the current usage level
        var level = MemoryManager.AppMemoryUsageLevel;
    
        // Check the usage level to determine whether reducing memory is necessary.
        // Memory usage may have been fine when initially entering the background but
        // the app may have increased its memory usage since then and will need to trim back.
        if (level == AppMemoryUsageLevel.OverLimit || level == AppMemoryUsageLevel.High)
        {
            ReduceMemoryUsage(MemoryManager.AppMemoryUsageLimit);
        }
    }

    Best Regards,

    Drake


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 20, 2019 7:10 AM
    Moderator

All replies

  • Hi,

    Thanks for posting here.

    To get global memory usage, you can use GlobalMemoryStatusEx to retrieves information about the system's current usage of both physical and virtual memory. You can get the approximate percentage of memory used, the amount of memory used, and the amount of memory available. Specify a threshold and determine whether the current memory state reaches the threshold.

    To get current app memory usage, use MemoryManager class, set app memory usage limit, and create app memory usage increased event.

    Best Regards,

    Drake


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 20, 2019 1:42 AM
    Moderator
  • I am using MemoryManager.AppMemoryUsageIncreased to get notified when there is a memory warning, but this event is getting triggered for each app launch, i am not sure how to deal with this. My requirement is to get notified when there is a OutOfMemory WARNING , so that i can save my data, before the app crash.

    Noorul.

    Friday, December 20, 2019 5:46 AM
  • You need to judge whether AppMemoryUsageLevel is OverLimit or High in event.

    like the sample in the document:

    private void MemoryManager_AppMemoryUsageIncreased(object sender, object e)
    {
        // Obtain the current usage level
        var level = MemoryManager.AppMemoryUsageLevel;
    
        // Check the usage level to determine whether reducing memory is necessary.
        // Memory usage may have been fine when initially entering the background but
        // the app may have increased its memory usage since then and will need to trim back.
        if (level == AppMemoryUsageLevel.OverLimit || level == AppMemoryUsageLevel.High)
        {
            ReduceMemoryUsage(MemoryManager.AppMemoryUsageLimit);
        }
    }

    Best Regards,

    Drake


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 20, 2019 7:10 AM
    Moderator
  • Exactly what i'm looking for, but there seems to be a bug, the event never get fired. Did i make something wrong or is it because i'm on debug mode? 
    Monday, January 20, 2020 8:45 AM