0

I am trying to load layout with a background image and a bitmap of full screen size but it is failing to allocate memory in motorola moto x pure edition with 3 gb of ram. But it loads fine if I load either the background image or the bitmap i.e. only one of them.

However, the layout loads without any issue in some other motorola devices, even those with 1gb of ram.

Size of the image is around 15 kb.

layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/parent_dino_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/dino_bg1"
    tools:context=".DinoActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/type_one"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">

        <ScratchView
            android:id="@+id/type_one_soil_layer"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/type_one_second_part"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/dino_bg2"
            android:visibility="gone">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHeight_percent="0.66"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.2">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </RelativeLayout>


        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@drawable/bone_bar"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHeight_percent="0.3">

            <LinearLayout
                android:id="@+id/type_one_bottom_bar"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:orientation="horizontal"
                android:paddingLeft="9dp"
                android:paddingTop="6dp"
                android:paddingRight="9dp"
                android:weightSum="8"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_percent="0.7"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintWidth_percent="0.84">

                <RelativeLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:padding="4dp">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:src="@drawable/bone_bar_cell" />

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:padding="4dp">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:src="@drawable/bone_bar_cell" />

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:padding="4dp">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:src="@drawable/bone_bar_cell" />

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:padding="4dp">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:src="@drawable/bone_bar_cell" />

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:padding="4dp">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:src="@drawable/bone_bar_cell" />

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:padding="4dp">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:src="@drawable/bone_bar_cell" />

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:padding="4dp">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:src="@drawable/bone_bar_cell" />

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:padding="4dp">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:src="@drawable/bone_bar_cell" />

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </RelativeLayout>

            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>


    <ImageView
        android:id="@+id/dino_hint"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:src="@drawable/hint_arrow"
        android:visibility="gone"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintHeight_percent="0.15"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/dino_ray"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:src="@drawable/rays2"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_percent="0.8"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_percent="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.somegame, PID: 4648
    java.lang.OutOfMemoryError: Failed to allocate a 13777932 byte allocation with 12240096 free bytes and 11MB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.Bitmap.nativeCreate(Native Method)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:905)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:882)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:849)
        at com.somegame.ScratchView.onSizeChanged(:64)
        at android.view.View.sizeChange(View.java:17656)
        at android.view.View.setFrame(View.java:17618)
        at android.view.View.layout(View.java:17535)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(:1915)
        at android.view.View.layout(View.java:17538)
        at android.view.ViewGroup.layout(ViewGroup.java:5616)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(:1915)
        at android.view.View.layout(View.java:17538)
        at android.view.ViewGroup.layout(ViewGroup.java:5616)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:17538)
        at android.view.ViewGroup.layout(ViewGroup.java:5616)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
        at android.view.View.layout(View.java:17538)
        at android.view.ViewGroup.layout(ViewGroup.java:5616)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:17538)
        at android.view.ViewGroup.layout(ViewGroup.java:5616)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
        at android.view.View.layout(View.java:17538)
        at android.view.ViewGroup.layout(ViewGroup.java:5616)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
        at android.view.View.layout(View.java:17538)
        at android.view.ViewGroup.layout(ViewGroup.java:5616)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2354)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2081)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1258)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6348)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
        at android.view.Choreographer.doCallbacks(Choreographer.java:683)
        at android.view.Choreographer.doFrame(Choreographer.java:619)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6123)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

1 Answers1

0

Bitmap process - Use of libraries such as Glide or Picasso is recommended. Relevant libraries use background caching algorithms.

Sample : LRUCache

I recommend using Glide.

Sample Code

Glide.with(this)
    .asBitmap()
    .load(imagePath)
    .into(object : CustomTarget<Bitmap>(){
        override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
            imageView.setImageBitmap(resource)
        }
        override fun onLoadCleared(placeholder: Drawable?) {
            // this is called when imageView is cleared on lifecycle call or for
            // some other reason.
            // if you are referencing the bitmap somewhere else too other than this imageView
            // clear it here as you can no longer have the bitmap
        }
    })
Arda Kazancı
  • 8,341
  • 4
  • 28
  • 50