0

Hi all I am developing live wallpapers, and I am using lot of bitmaps. I have tested my new live wallpaper for aboute a week, and it was up and runing perfectly, but as soon as I have uploaded it to a market I keep getting this kind of exceptions : java.lang.OutOfMemoryError for both android.graphics.Bitmap.nativeCreate and android.graphics.BitmapFactory.nativeDecodeAsset. I use this kind of lifecycle of an bitmap:

I create a reference like:

Bitmap dark = null;
Bitmap cave = null;

at onCreateEngine I init them like :

cave = BitmapFactory.decodeResource(getResources(), R.drawable.cave);
dark = BitmapFactory.decodeResource(getResources(), R.drawable.dark);

here is where it trows the exception. for these images: backgrounddarkeningclouds. and after all I draw them to an canvas like this:

canvas.save();
canvas.drawBitmap(bg, new Matrix(), new Paint());   
canvas.drawBitmap(dark, new Matrix(), new Paint()); 
canvas.restore();

What Should I do? It is better to load the dark image just one picture and draw it to the canvas width*height times? Or Are there any methods to do? I think recycle or calling onDestroy. But I do not know when to call them because the exceptions are thrown at onCreate.Are the images too big? And why it is working smoothly on my device, and on the other devices it is throwing exceptions? The bitmaps are 1484*1484 dimension big and the clouds are 250*172 dimensional big, should they be in 2^x * 2^x dimension?

1 Answer 1

1

Just try to use Memory Optimizer and see where are you creating Memory Leaks. You can use Eclipse Memory Analyzer(MAT) for this. Its a very common problem with using bitmaps. By using BitMaps you need to be extra careful for memory leaks.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks I will check it, but if I found somethink, what and WHERE should I do? at onPause? or onDestroy?
I dont think so.. onPause or onDestroy will work. You will have to release the memory on every iteration see this : androidcocktail.blogspot.in/2012/05/…
I have implemented this methods and it gives back a smaller bitmaps, the question is that it is garanted that with this kind of methods I wont get memory exceptions?
Have you tried Eclipse Memory Analyzer. Actually you should check your logcat as well. See GC specific log. Your GC should be getting increase whenever you load more and more image. You GC should show the constant heapsize. If you achieve this It's guarantee that your app wont show up OutOfMemory issue.
Yes thanks I have implemented the decoder, and tested it, it is up and runing without any crashes since than thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.