Question

my app (memory game) is using various images. In my /drawable/ folder are about ~50 .png images with 252x252 size. My app has some levels, for example 4x3, 4x6 etc. After 3-4 games (for example, I started from 2x2 to 4x6) - my app is crashing (Unfortunately, your app has stopped). In LogCat I have these errors:

04-16 23:05:35.382: E/AndroidRuntime(13622): FATAL EXCEPTION: main
04-16 23:05:35.382: E/AndroidRuntime(13622): java.lang.OutOfMemoryError
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.graphics.Bitmap.nativeCreate(Native Method)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:530)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:505)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:357)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:777)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.content.res.Resources.loadDrawable(Resources.java:1940)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.content.res.Resources.getDrawable(Resources.java:669)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.view.View.setBackgroundResource(View.java:11861)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at com.example.testlogo.MainActivity.onCreate(MainActivity.java:145)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.app.Activity.performCreate(Activity.java:4465)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.os.Looper.loop(Looper.java:137)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at android.app.ActivityThread.main(ActivityThread.java:4448)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at java.lang.reflect.Method.invoke(Method.java:511)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
04-16 23:05:35.382: E/AndroidRuntime(13622):    at dalvik.system.NativeStart.main(Native Method)

As I said, I am using images from /drawable/ folder. To make for example 4x3 level, I am using GridLayout and ImageButtons. I have a method, which randomizes ImageButton's place and then I am putting the image to the ImageButton, like this:

button1.setImageResource(R.drawable.logo);

For resize my ImageButton I am using:

    Display display = getWindowManager().getDefaultDisplay(); 
        int screenwidth = display.getWidth();
        int screenheight = display.getHeight(); 
            button.setMaxHeight(screenwidth/3);
            button.setMaxWidth(screenwidth/3);

So I do not really know, how to manage with my problem. Maybe should I use Bitmap instead of ImageResource?

Note: 1)Every level have own class, they are very similar to each other.

2)Every level class have 24 images. If the level is 4x3 I will get first random 6 pics.

3)Every activity are using finish(); method if the activity is closed/onBackPressed.

int[] img ={R.drawable.cherry90,R.drawable.apple90,R.drawable.applered90,R.drawable.apricot90,R.drawable.banana90,R.drawable.blueberry90,


R.drawable.coconut90,R.drawable.gooseberry90,R.drawable.grape90,R.drawable.grapefruit90,R.drawable.kiwi90,R.drawable.lemon90,
                R.drawable.lime90,R.drawable.mandarin90,R.drawable.mango90,R.drawable.melon,R.drawable.papaya90,R.drawable.peach90,
                R.drawable.pears90,R.drawable.pineapple,R.drawable.plums90,R.drawable.pomegranate90,R.drawable.raspberry90,R.drawable.strawberry90};

I hope you will understand what I wanted to say. I were searching an answer to this problem, but did not find.

Was it helpful?

Solution

Basically it an error that indicates you have used excess of your ram memory allocations, there is nothing wrong in your code it just needs to be optimized. Here are a few ways to do this:

  • Reduce the resolution of the images, you can still stretch them to the same size, but they wont show in as much detail.
  • Delete or re-use image variable space.
  • If you show the same image multiple times on the screen, like with most tiled maps. only create one variable where the image is stored.

Remember variables mean ram.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top