-1

I am doing android app, works fine in a device with 3Gb Ram but bad in a device with 512 mbRam. I am getting error "bitmap size exceeds VM budget" I don´t know how solve it. This is my layout´s code that uses many drawables.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/fondohis"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#333"
        android:gravity="bottom"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/container_2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/fondo_menu"
        android:orientation="vertical" >

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/transparent_negro"
            android:scrollbars="none" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5"
                        android:gravity="center_vertical|center_horizontal"
                        android:orientation="vertical" >

                        <ImageView
                            android:id="@+id/image_episodio1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/athle" />

                        <TextView
                            android:id="@+id/episodio1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Los orígenes"
                            android:textColor="#FFFFFF" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5"
                        android:gravity="center_vertical|center_horizontal"
                        android:orientation="vertical" >

                        <ImageView
                            android:id="@+id/image_episodio2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/pentl" />

                        <TextView
                            android:id="@+id/episodio2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Primera"
                             android:textColor="#FFFFFF" />
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                     >

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5"
                        android:orientation="vertical"
                        android:gravity="center_vertical|center_horizontal" >

                        <ImageView
                            android:id="@+id/image_episodio3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/delant" />

                        <TextView
                            android:id="@+id/episodio3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Segund"
                             android:textColor="#FFFFFF" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5"
                        android:orientation="vertical" 
                        android:gravity="center_vertical|center_horizontal">

                        <ImageView
                            android:id="@+id/image_episodio4"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/cambios_mini" />

                        <TextView
                            android:id="@+id/episodio4"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="É"
                             android:textColor="#FFFFFF" />
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5"
                        android:orientation="vertical"
                        android:gravity="center_vertical|center_horizontal" >

                        <ImageView
                            android:id="@+id/image_episodio5"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/niev" />

                        <TextView
                            android:id="@+id/episodio5"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Debut"
                             android:textColor="#FFFFFF" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5"
                        android:orientation="vertical"
                        android:gravity="center_vertical|center_horizontal" >

                        <ImageView
                            android:id="@+id/image_episodio6"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/aldeanos" />

                        <TextView
                            android:id="@+id/episodio6"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="La"
                             android:textColor="#FFFFFF" />
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5"
                        android:orientation="vertical"
                        android:gravity="center_vertical|center_horizontal" >

                        <ImageView
                            android:id="@+id/image_episodio7"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ir" />

                        <TextView
                            android:id="@+id/episodio7"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="ss"
                             android:textColor="#FFFFFF" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5"
                        android:orientation="vertical"
                        android:gravity="center_vertical|center_horizontal">

                        <ImageView
                            android:id="@+id/image_episodio8"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/fin" />

                        <TextView
                            android:id="@+id/episodio8"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="kk" 
                             android:textColor="#FFFFFF"/>
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5"
                        android:gravity="center_vertical|center_horizontal"
                        android:orientation="vertical" >

                        <ImageView
                            android:id="@+id/image_episodio9"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/gaba" />

                        <TextView
                            android:id="@+id/episodio9"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="em"
                             android:textColor="#FFFFFF" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.5"
                        android:gravity="center_vertical|center_horizontal"
                        android:orientation="vertical" >

                        <ImageView
                            android:id="@+id/image_episodio10"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/bie" />

                        <TextView
                            android:id="@+id/episodio10"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="yy" 
                             android:textColor="#FFFFFF"/>
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>

        </ScrollView>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/container_iribar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/fondohis"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/historiacuatro" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/container_zarra"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/fondohis"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/historiatres" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/container_pichichi"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/fondohis"
        android:gravity="right|center_vertical"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/historiados" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/container_histo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/fondohis"
        android:gravity="bottom|left"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/icono_comentario"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/historiauno" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/menu_capa"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerVertical="true"
        android:background="@drawable/fondohis"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:visibility="gone" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/transparent_negro"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/titulo_portada"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/circle"
                android:text="Gure"
                android:textColor="#FFFFFF"
                android:textSize="50sp" />

            <TextView
                android:id="@+id/titulo_anos"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textColor="#ffffff"
                android:textSize="40sp" />

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

I have using this that I have read in other questions but I can´t solve it:

 @Override
        protected void onDestroy() {
            super.onDestroy();

            unbindDrawables(findViewById(R.id.RootView));
            System.gc();
        }

        private void unbindDrawables(View view) {
            if (view.getBackground() != null) {
                view.getBackground().setCallback(null);
            }
            if (view instanceof ViewGroup) {
                for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                    unbindDrawables(((ViewGroup) view).getChildAt(i));
                }
                ((ViewGroup) view).removeAllViews();
            }
        }
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
Elena
  • 181
  • 2
  • 12
  • In which folder have you kept the drawables (nodpi)?? – dudego Feb 28 '15 at 12:05
  • yes in nodpi, is this bad practice? – Elena Feb 28 '15 at 12:15
  • this will never be scaled by ImageView so it will allocate the memory of the same size as of drawable hence the exception. My suggestion try to scale the image properly or extend imageview and override onDraw method to write scaling algo... I will try to share the code if I have time... – dudego Feb 28 '15 at 12:19
  • For starters just move the drawable to hdpi folder and check if it crashes... If this helps then you can save scaled images by the time... – dudego Feb 28 '15 at 12:21
  • I have moved drawables to hdpi and still crashing – Elena Feb 28 '15 at 12:31
  • whats the resolution(width*height), average-size of drawables? – dudego Feb 28 '15 at 12:34
  • 640x911 81.198 bytes for example – Elena Feb 28 '15 at 12:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71941/discussion-between-sahild007-and-elena). – dudego Feb 28 '15 at 12:37

4 Answers4

2

You can compress your bitmaps when you display Bitmap on ImageView.

image.buildDrawingCache();
Bitmap img = image.getDrawingCache();

ByteArrayOutputStream stream = new ByteArrayOutputStream();
img.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] food = stream.toByteArray();

//display Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();

Aspicas
  • 4,498
  • 4
  • 30
  • 53
0

Ram is not related to VM budget. You must reduce your bitmap size. Of course adding this line:

android:largeHeap="true"

in your application tag in manifest may help a little but it doesn't solve the problem.

for more information I can give you this link which completely explained the issue and I can not add anything more to that: Strange out of memory issue while loading an image to a Bitmap object

You must do something like this :

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);

Also see this link: https://stackoverflow.com/a/17990482/2101822

hope it helps ;)

Community
  • 1
  • 1
Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
0

1. For dealing with Bitmap objects:

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
options.inDither = false;
options.inPurgeable = true;
options.inInputShareable = true;
Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);

Also, you need to use

bitmap.recycle();

before you make your Bitmap instances null. This will help save memory.

2. For dealing with images in res/drawable:

  • Add the android:largeHeap=“true” attribute to the application tag in your project's manifest file.
  • Resize your images with a graphics editor and reduce their size & dimension.

Note: One of the answers says that "This error will only be visible in logcat, it won't affect your application." So yes, that is entirely false. An OutOfMemoryError is a Java Error, even more pernicious than a RuntimeException. It will bring the app to its knees.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • Attribute "largeHeap" is only used in API level 11 and higher (current min is 8), I am using 8 – Elena Feb 28 '15 at 12:11
  • see edited answer ... I am currently taking a look at the project on which I worked on this and will post more when I find more info :) – Yash Sampat Feb 28 '15 at 12:14
  • 1
    @Elena caution: larger the heap more frequent gc and more pause times. Its not needed in most cases. You can scale down the image. http://developer.android.com/training/displaying-bitmaps/load-bitmap.html – Raghunandan Feb 28 '15 at 12:21
  • @Elena: please see edited answer. While Raghunandan is right, you can use a combination of techniques to control avoiding this error. – Yash Sampat Feb 28 '15 at 12:25
  • No, because I don´t know where I have to put this, I don´t set the drawables programmatically, I set in the layout. – Elena Feb 28 '15 at 13:35
  • the xml is posted, the code I can´t , it says that the body can´t contains more than x words, perhaps in chat? – Elena Feb 28 '15 at 13:45
  • ok i see that you are setting images directly from the `drawables` folder .... in this case you can do two things: **1.** resize your images with a graphics editor and reduce their size & dimension. **2.** add `android:largeHeap="true"` in your manifest. – Yash Sampat Feb 28 '15 at 13:52
  • The second thing I can´t because android:largeHeap="true" is only used in API level 11 and higher (current min is 8), I am using 8. And the first, my images have 640x911 and 100KB size, is this a big size?? – Elena Feb 28 '15 at 13:56
  • Its not a *very* big size, but since you have a lot of images in that layout and you are probably using a device with API level < 10 for testing, it tends to blow up. You could consider supporting only API 11+ devices, as most people are now using these :) – Yash Sampat Feb 28 '15 at 13:59
0

Increase your heap size.

In AndroidManifest.xml, add the android:largeHeap=“true” attribute to the Application tag.

  • 1
    Attribute "largeHeap" is only used in API level 11 and higher (current min is 8), I am using 8 – Elena Feb 28 '15 at 12:11