Steve
Steve

Reputation: 393

DidReceiveMemoryWarning in IOS using Xamarin Forms

How can you catch the DidReceiveMemoryWarning using xamarin forms.

I can see them in the application output when debugging in xamarin studio, but i cant find how to catch the event or how to see how much memory is used.

I tried AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize but it throws a not implemented exception

Upvotes: 5

Views: 2175

Answers (3)

Vahida Havaldar
Vahida Havaldar

Reputation: 524

Easiest way is to override ReceiveMemoryWarning in AppDelegate.cs as follows-

    [Export("applicationDidReceiveMemoryWarning:")]
    public override void ReceiveMemoryWarning(UIApplication application)
    {
        try
        {
            base.ReceiveMemoryWarning(application);
            Console.WriteLine("****************************************************************");
            Console.WriteLine("****************RECIEVED MEMORY WARNING***********************");
            Console.WriteLine("****************************************************************");
            Console.WriteLine("TOTAL MEMORY {0}",GC.GetTotalMemory(false));

            AnalyticsService.Current.TrackUserActions("RECIEVED MEMORY WARNING");
        }
        catch (Exception ex)
        {
            AnalyticsService.Current.TrackException(ex);
        }
    }

Every time iOS running on low memory this method will get called and will crash app. Total memory(RAM) allocated/available can be checked by calling this method GC.GetTotalMemory(false) after app launch or wherever you want to check the available memory.

Upvotes: 0

Has AlTaiar
Has AlTaiar

Reputation: 4152

There are 3 ways to capture Memory Warnings in iOS (or at least this what I know of :)

These three ways are:

  1. In your ViewControllers, you could override DidReceiveMemoryWarning() and handle the warning there. This is not really the best way for Xamarin.Forms as you do not have UIViewController to override these methods, so move on to options 2 and 3.

  2. In your AppDelegate, override ReceiveMemoryWarning method. This will get fired when the iOS is running low on memory. You could wire this method to any code you have on your PCL code or just handle it in your platform-specific project.

    public override void ReceiveMemoryWarning (UIApplication application)
    {
            // handle low memory warnings here
    }
    
  3. You could use iOS NotificationCentre to receive a notification when there is a memory warning. This could be done like this:

    // Method style void Callback (NSNotification notification) 
    {
            Console.WriteLine ("Received a notification UIApplication", notification);
    }
    
    void Setup () 
    {
            NSNotificationCenter.DefaultCenter.AddObserver (UIApplication.DidReceiveMemoryWarningNotification, Callback); 
    }
    

You could then wire this "CallBack" to your PCL project to free up some memory.

You could also test this on the simulator using

Hardware >> Simulate Memory Warnings

Upvotes: 7

Stephane Delcroix
Stephane Delcroix

Reputation: 16232

You can override DidReceiveMemoryWarning in you iOS project, and from there notify the Xamarin.Forms pages. I can think of many ways to achieve this, but here are the 2 more obvious:

Upvotes: 1

Related Questions