Reputation: 393
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
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
Reputation: 4152
There are 3 ways to capture Memory Warnings in iOS (or at least this what I know of :)
These three ways are:
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.
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
}
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
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:
DependencyService
as a DI container, but you can use the one you want: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/Upvotes: 1