Huge memory usage in Xamarin

I have some problems running my application on some older Android devices, and so I downloaded the Visual Studio Professionel trace as it has Diagnostics Tools .

I tried doing some simple things in my application, and I find it scary that Xamarin.Forms.BindableProperty+BindablePropertyContext takes a size (in bytes of course) of 2.196.088 in UWP, which you can see on the next screendump.

UWP Managed Memory .

In the example that I followed, you can go through 5 pages. There are ListViews on 2 pages, and one of them has been cleared 3 times and filled with new data.

So, do I need to call GC.Collect() after clearing the ListView ?

+5
source share
1 answer

I had a similar problem - several times page navigation threw an OutOfMemoryException. For me, the solution was to implement custom rendering for a page with an explicit call to Dispose ().

 public class CustomPageRenderer : PageRenderer { private NavigationPage _navigationPage; protected override void OnElementChanged(ElementChangedEventArgs<Page> e) { base.OnElementChanged(e); _navigationPage = GetNavigationPage(Element); SubscribeToPopped(_navigationPage); } private void SubscribeToPopped(NavigationPage navigationPage) { if (navigationPage == null) { return; } navigationPage.Popped += OnPagePopped; } protected override void Dispose(bool disposing) { Log.Info("===========Dispose called==========="); base.Dispose(disposing); } private void OnPagePopped(object sender, NavigationEventArgs args) { if (args.Page != Element) { return; } Dispose(true); _navigationPage.Popped -= OnPagePopped; } private static NavigationPage GetNavigationPage(Element element) { if (element == null) { return null; } while (true) { if (element.Parent == null || element.Parent.GetType() == typeof(NavigationPage)) { return element.Parent as NavigationPage; } element = element.Parent; } } } 

You can also look here , but you need to be careful with deleting images, this can cause some problems if their parent page is in the navigation stack and you want to return.

+1
source

Source: https://habr.com/ru/post/1265596/


All Articles