1

We were making a program on Android Studio, everything was fine, but then, We started having problems with memory in an specific testing phone, it is a MOTO G4 PLUS, ANDROID 7.0, API 24, and when we start, it uses about 29GB.

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.wende.memeadventures, PID: 16490
                  java.lang.OutOfMemoryError: Failed to allocate a 29160012 byte allocation with 803264 free bytes and 784KB until OOM

But on every other testing cellphone, it worked perfectly, we removed some of the images used on the program, and the problem was solved, but we need the images, and we don´t know what to do about it, we tried to compress the images but it didn´t worked.

Another cellphone that was used is a J2, Android 5.1.1 API 22, and it worked perfectly. It doesn't make any sense for me.

This is my main activity layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".Activities.MainActivity">

    <Button
        android:id="@+id/Click_btn"
        android:layout_width="0dp"
        android:layout_height="421dp"
        android:layout_marginBottom="68dp"
        android:layout_marginTop="8dp"
        android:background="@null"
        android:text="@null"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/top_Bar_iv" />

    <ImageView
        android:id="@+id/Bot_Bar_iv"
        android:layout_width="match_parent"
        android:layout_height="164dp"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/painel_down" />

    <ImageView
        android:id="@+id/top_Bar_iv"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@android:color/holo_green_dark" />

    <ImageView
        android:id="@+id/Money_bg_iv"
        android:layout_width="168dp"
        android:layout_height="53dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="@+id/top_Bar_iv"
        app:layout_constraintStart_toStartOf="@+id/top_Bar_iv"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/moneycash_bg" />

    <ImageView
        android:id="@+id/Cash_bg_iv"
        android:layout_width="113dp"
        android:layout_height="53dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="@+id/top_Bar_iv"
        app:layout_constraintEnd_toEndOf="@+id/Cash_tv"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/Money_bg_iv"
        app:layout_constraintTop_toTopOf="@+id/top_Bar_iv"
        app:srcCompat="@drawable/moneycash_bg" />

    <TextView
        android:id="@+id/Money_tv"
        android:layout_width="94dp"
        android:layout_height="26dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="30dp"
        android:layout_marginTop="9dp"
        android:gravity="center"
        android:text="Dinheiro"
        app:layout_constraintBottom_toBottomOf="@+id/top_Bar_iv"
        app:layout_constraintStart_toStartOf="@+id/top_Bar_iv"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/Cash_tv"
        android:layout_width="68dp"
        android:layout_height="25dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="64dp"
        android:layout_marginTop="7dp"
        android:gravity="center"
        android:text="Cash"
        app:layout_constraintBottom_toBottomOf="@+id/top_Bar_iv"
        app:layout_constraintStart_toEndOf="@+id/Money_tv"
        app:layout_constraintTop_toTopOf="@+id/top_Bar_iv"
        app:layout_constraintVertical_bias="0.533" />

    <ImageButton
        android:id="@+id/Cash_btn"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="10dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@null"
        android:onClick="cash_click"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toBottomOf="@+id/top_Bar_iv"
        app:layout_constraintEnd_toEndOf="@+id/Cash_bg_iv"
        app:layout_constraintStart_toEndOf="@+id/Cash_tv"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.56"
        app:srcCompat="@mipmap/ic_launcher_round" />

    <ImageButton
        android:id="@+id/money_btn"
        android:layout_width="33dp"
        android:layout_height="31dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@null"
        android:onClick="money_click"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toBottomOf="@+id/top_Bar_iv"
        app:layout_constraintStart_toEndOf="@+id/Money_tv"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/meme_coin" />

    <ImageButton
        android:id="@+id/Config_btn"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="8dp"
        android:background="@null"
        android:onClick="config_click"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toBottomOf="@+id/top_Bar_iv"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.888"
        app:srcCompat="@drawable/config" />

    <ImageButton
        android:id="@+id/Upgrades_btn"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:background="@null"
        android:onClick="upgrades_click"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.33"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/Bot_Bar_iv"
        app:layout_constraintVertical_bias="0.925"
        app:srcCompat="@drawable/botao_upgrade" />

    <ImageButton
        android:id="@+id/Reset_btn"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:background="@null"
        android:onClick="reset_click"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/Bot_Bar_iv"
        app:layout_constraintVertical_bias="0.925"
        app:srcCompat="@drawable/botao_reset" />

    <ImageButton
        android:id="@+id/CashStore_btn"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:background="@null"
        android:onClick="cashstore_click"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/Bot_Bar_iv"
        app:layout_constraintVertical_bias="0.925"
        app:srcCompat="@drawable/botao_dinheiro" />

    <ImageButton
        android:id="@+id/Quests_btn"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:background="@null"
        android:onClick="quests_click"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.67"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/Bot_Bar_iv"
        app:layout_constraintVertical_bias="0.925"
        app:srcCompat="@drawable/botao_missao" />

</android.support.constraint.ConstraintLayout>

Sorry about anything. It's my first question here and my english may not be perfect. And thank you for trying to help.

Float
  • 25
  • 1
  • 9
  • Please post the code of loading images and thedirectory of images – Motee Aug 10 '18 at 02:51
  • The images are in this directory: app\src\main\res\drawable. I really don't know if it's exactly what you are asking for, if it's really wrong, could you be more specific please? We still learning, so sorry for the lack of experience – Float Aug 10 '18 at 03:19
  • instead of doing `app:srcCompat`, can u try `android:src`? I know it sounds stupid but sometimes back to basic works – Julfikar Aug 10 '18 at 03:49

2 Answers2

0

It sounds to me that the cellphone where this fails does not have enough memory.

AFAIK there is no way to process an entire image (at once) with less memory than the image requires.

  • You could try changing the image resolution; i.e. reducing the pixel count.
  • You could try splitting the images into subimages (tiles) and processing them one at a time.

Compression won't work, because compressed images have to be uncompressed in memory to do anything with them; e.g. to display them. Downscaling and tiling require you to do some image processing before the images land on the device.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • The problematic cellphone has 8GB ram, more than the another where the program runs smoothly. Could this still be the problem? and how could I do the second option you gave me? (Splitting the images). And thanks for the awnser. – Float Aug 10 '18 at 03:42
0

The image is too large and hdpi xhdpi xxhdpi will load different size of images.You could get the size by the demo

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
Bitmap.Config preferredConfig = options.inPreferredConfig;
LogUtils.d("size is"+(imageHeight*imageWidth*getBytesPerPixel(preferredConfig)/1024/1024)+"MB");

You should learn more about BitmapFactory.decodeResource,here is the link. And your problom can find a solution from image OOM

Motee
  • 112
  • 7
  • I will take a look. Thank you very much for the help, looks like it's really the problem. But why it happens only in that cellphone (with more advanced API and higher memory)? – Float Aug 10 '18 at 03:47
  • it will load different size of image from ```hdpi```、 ```xhdpi``` or ```xxhdpi``` depending the density of mobile – Motee Aug 10 '18 at 03:53
  • This will certainly help me. Thank you very much for the help. – Float Aug 10 '18 at 03:54