I am working on application which uses downloaded images from my application to display in the background. When I start application it works fine and load image without any problem. But after several restart application crashes with memory error. I use below method for get Drawable to show it in View. I have used one class and which having this static method I use it for all images. I also call one other static method for clear bitmap but don’t know it works or not. May be I do not able to clear memory when I close application.
public static void recycle_bitmap() {
if (myBitmap != null) {
myBitmap.recycle();
myBitmap = null;
}
}
static Bitmap myBitmap = null;
public static Drawable ImgDrawableFromFile(Resources res, String file_name) {
myBitmap=null;
File imgFile = new File("/data/data/com.appstart/app_my_sub_dir/"
+ file_name + ".jpg");
if (imgFile.exists()) {
myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
if (myBitmap != null)
return new BitmapDrawable(res, myBitmap);
else
return null;
}
return null;
}
Logcat error
01-11 12:09:41.860: D/dalvikvm(6047): GC_EXTERNAL_ALLOC freed 598K, 48% free 3336K/6407K, external 8683K/10523K, paused 274ms
01-11 12:09:42.149: E/dalvikvm-heap(6047): 1671840-byte external allocation too large for this process.
01-11 12:09:42.480: I/dalvikvm-heap(6047): Clamp target GC heap from 16.094MB to 16.000MB
01-11 12:09:42.480: E/GraphicsJNI(6047): VM won't let us allocate 1671840 bytes
01-11 12:09:42.501: D/dalvikvm(6047): GC_FOR_MALLOC freed 1K, 48% free 3334K/6407K, external 8674K/10523K, paused 247ms
01-11 12:09:42.501: D/skia(6047): --- decoder->decode returned false
01-11 12:09:42.501: W/dalvikvm(6047): threadid=15: thread exiting with uncaught exception (group=0x40015560)
01-11 12:09:42.560: E/AndroidRuntime(6047): FATAL EXCEPTION: Thread-18
01-11 12:09:42.560: E/AndroidRuntime(6047): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
01-11 12:09:42.560: E/AndroidRuntime(6047): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
01-11 12:09:42.560: E/AndroidRuntime(6047): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)
01-11 12:09:42.560: E/AndroidRuntime(6047): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:284)
01-11 12:09:42.560: E/AndroidRuntime(6047): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:309)
01-11 12:09:42.560: E/AndroidRuntime(6047): at com.appstart.utility.LoadImage.ImgDrawableFromFile(LoadImage.java:30)
01-11 12:09:42.560: E/AndroidRuntime(6047): at com.appstart.MainActivity.run(MainActivity.java:70)
01-11 12:09:42.560: E/AndroidRuntime(6047): at java.lang.Thread.run(Thread.java:1019)
Follows these three tutorials to avoid Memory error.
Avoid Memory Leaks
Attacking Memory Problems on Android
Future-Proofing Your App
and In particular, using static variables is likely to make things worse, not better.
so please remove it.and put it outside static method.
it may be also cause due to large image size.please scale it to small size.or increase your Vm’s heapsize.
it might help you.