1

I'm very new to Xamarin.Forms (2 weeks) so I've been running into many errors, warnings, etc...
Right now I'm trying to run an app with just one page but there are 5 images that are being used as background for buttons and that's it.

But every time I run the android app on my phone or the emulator (Windows app runs fine), I get the "OutofMemory Error"... I'm lost here, I don't even have so much code on it...

If anyone could tell me what I'm doing wrong or give me a clue on how to solve it, I'll really appreciate it!!

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:PAEC"
         x:Class="PAEC.MainPage">

     <Grid >
         <Image Source="background.png" Aspect="AspectFill"/>
         <StackLayout VerticalOptions="Center" HorizontalOptions="Fill">

             <Button
                Image="Circle_Green.png"
                BackgroundColor="Transparent"
                />
             <Button
                Image="Circle_Red.png"
                BackgroundColor="Transparent"
                />
             <Button
                Image="Circle_Blue.png"
                BackgroundColor="Transparent"
                />
             <Button
                Image="Circle_Grey.png"
                BackgroundColor="Transparent"
                />
             <Button
                Image="Circle_Orange.png"
                BackgroundColor="Transparent"
                />

         </StackLayout>
    </Grid>
</ContentPage>

MainPage.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace PAEC
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
             InitializeComponent();
        }
    }
}

Output

1 Answer 1

1

Android converts all images into a raw bitmap when displaying, hence it uses a ton of memory to do so. Windows and iOS don't do this, which is why it will work fine on those platforms.

In order to counteract this you need to make sure your images are the correct size that is being displayed. If you have an image that is 1000x1000 then it will create a raw bitmap for that whole image size, even if its only showing as 48x48 on the screen.

The other thing to do, is to go into your properties of the Android project, and set your Java Max Heap Size to 1G

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.