Csabi
Csabi

Reputation: 3043

Avoiding java.lang.OutOfMemoryError

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?

Upvotes: 0

Views: 178

Answers (1)

Brijesh Thakur
Brijesh Thakur

Reputation: 6778

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.

Upvotes: 1

Related Questions