mxxyyy
mxxyyy

Reputation: 11

How to fix Java.Lang.OutOfMemoryError in Xamarin.Forms android application?

So, i have some problems with my Xamarin.Forms app. I am getting "Java.Lang.OutOfMemoryError" when I am trying to await ViewExtensions.LayoutTo.

I have tried to increase Java Heap size up to 2G, but it did not help.

public class SplashPage : ContentPage
    {
    public SplashPage()
        {
            NavigationPage.SetHasNavigationBar(this, false);

            AbsoluteLayout layout = new AbsoluteLayout();
            Content = layout;

            Image image1 = new Image
            {
                Source = ImageSource.FromFile("image1.png"),
            };
            layout.Children.Add(image1);

            Image image2 = new Image
            {
                Source = ImageSource.FromFile("image2.png"),
            };
            layout.Children.Add(image2);

            Image image3 = new Image
            {
                Source = ImageSource.FromFile("image3.png"),
                Scale = 0.5,
            };
            layout.Children.Add(image3);

            layout.SizeChanged += async (s, e) =>
            {
                AbsoluteLayout.SetLayoutBounds(image1, new Rectangle(0, 0, image1.Width, image1.Height));
                AbsoluteLayout.SetLayoutBounds(image2, new Rectangle(0, ((AbsoluteLayout)s).Height, image2.Width, image2.Height));
                AbsoluteLayout.SetLayoutBounds(image3, new Rectangle(-image3.Width, ((AbsoluteLayout)s).Height - image3.Height - 5, image3.Width, image3.Height));

                // I have placed those animations in SizeChanged event because i needed to get actual size of layout (outside this event it would be incorrect)
                await ViewExtensions.LayoutTo(image2, new Rectangle(0, ((AbsoluteLayout)s).Height - image2.Height, image2.Width, image2.Height), 2300);
        // here Java.Lang.OutOfMemoryError
                await ViewExtensions.LayoutTo(image3, new Rectangle(((AbsoluteLayout)s).Width, ((AbsoluteLayout)s).Height - image3.Height - 5, image3.Width, image3.Height), 3000);

                await Task.WhenAll(
                    image2.FadeTo(0, 700), image1.FadeTo(0, 700));

                Application.Current.MainPage = new NavigationPage(new LoginPage());
            };
        }
    }

Upvotes: 1

Views: 380

Answers (1)

zpouip
zpouip

Reputation: 787

Try this way to increase your app's heap size in your AndroidManifest.xml:

<application android:largeHeap="true"></application>

Upvotes: 1

Related Questions