Torch2424
Torch2424

Reputation: 168

Making an android game with many images, how to avoid java.lang.OutOfMemory errors with many images

I've been searching Google, Stack Overflow, and the android developer's guide and yet, I cannot get a good solution to this problem.

I've tried the unbind drawables method:

      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();
    }
 }

And I can't use the loading large bitmaps efficiently from the android documentation, since the size I made the images is the maximum size (740 x 1140 pngs) that I want, and I don't know what other sizes I could plug into the call:

        mImageView.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));

Also, every activity has a 740x1140 png background, with other smaller image views for enemies and buttons and things. the most I have on screen at a time is about 8. I assign all of these images through XML. However, i finish every activity after I start a new one, yet it seems as if no memory is being freed.

Below is my beginning layout and logcat.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bro_quest_start_bg"
android:orientation="vertical"
android:scaleType="centerCrop"
tools:context="com.torch2424.broquest.StartScreen" >

<ImageView
    android:id="@+id/broquesttitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/rrppgg_title"
    android:contentDescription="@string/tank" />

<Button
    android:id="@+id/start"
    android:layout_width="120dp"
    android:layout_height="55dp"
    android:background="@drawable/buttons"
    android:onClick="fight"
    android:layout_gravity="center_horizontal"
    android:text="@string/start" />

 <Button
    android:id="@+id/journal"
     android:layout_width="120dp"
    android:layout_height="55dp"
    android:background="@drawable/buttons"
     android:layout_gravity="center_horizontal"
    android:onClick="journal"
    android:text="@string/journal" />

   <Button
    android:id="@+id/character"
     android:layout_width="120dp"
    android:layout_height="55dp"
    android:background="@drawable/buttons"
     android:layout_gravity="center_horizontal"
    android:onClick="character"
    android:text="@string/character" />

<Button
    android:id="@+id/shop"
    android:layout_width="120dp"
    android:layout_height="55dp"
    android:background="@drawable/buttons"
     android:layout_gravity="center_horizontal"
    android:onClick="shop"
    android:text="@string/shop" />


<Button
    android:id="@+id/options"
     android:layout_width="120dp"
    android:layout_height="55dp"
    android:background="@drawable/buttons"
     android:layout_gravity="center_horizontal"
    android:onClick="options"
    android:text="@string/options" />

<Button
    android:id="@+id/quit"
     android:layout_width="120dp"
    android:layout_height="55dp"
     android:layout_gravity="center_horizontal"
    android:background="@drawable/buttons"
    android:onClick="quit"
    android:text="@string/quit" />

Link to Logcat, Log cat format would not play nice with stack overflow :( http://pastebin.com/dZSPaq4d

P.S the images are all drawables in my drawable folder

Upvotes: 1

Views: 175

Answers (2)

Torch2424
Torch2424

Reputation: 168

Like Voicu says above, I was recieving OOM errors because my drawables are in the wrong folder. I was browsing the internet last night, and found this question deep in the google results: Android setBackgroundResource cause out of memory excepiton

Where, he described how he moved his images from the drawable folder to the drawable-nodpi folder and it solved all of his OOM problems. So I tried it for my app, and it worked as well. However, Images do look a bit different, so to figure out why this works, let us quote some of the Android documentation:

"The resources in drawable/ are the default drawable resources. The system assumes that default resources are designed for the baseline screen size and density, which is a normal screen size and a medium density. As such, the system scales default density resources up for high-density screens and down for low-density screens, as appropriate."

So, in my case, I was putting "HD" images for an "HD" Phone in the folder meant for "SD" images and phones since that what drawables/ defaults too. So in turn, it would scale all of my "HD" images bigger than they actually were and needed to be, which not only made them look worse, but also would make them use WAY more memory on the heap causing OOM (Out of Memory) Errors in my app.

For more info on the drawables folder and DPI see this link of Android Docs: http://developer.android.com/guide/practices/screens_support.html

P.S Unbind Drawables in OnDestroy() of every activity does help with memory, so If you are having any memory problems I suggest you try this. And with this call, people often call System.gc() after, which does help, but it is bad coding practice. But in my situation it was specifically the /drawables thing. Also, I stumbled upon tinypng.com to reduce PNG sizes, it does not help with OOM errors at all, but it does reduce your apk size, so I suggest you do this for your games and things since it lessens the file size with almost no noticeable difference.

Upvotes: 1

Karim Varela
Karim Varela

Reputation: 7652

Try using an image caching library like Picasso.

Upvotes: 1

Related Questions