Actually I am facing Out of Memory error in my app.:( the problem is that I have been using number of activities in my app and now at this point I can't move to another approach..:( So after flipping 27 or 29 activties i get OOM error on 30 activity on setContentView(). In my all XML files I have been using AbsoluteLayout setting background image with a main page for each Layout. And in every layout I have been setting 4 to 5 buttons with background having image. now every button is set with an image with sound.
-
Try solving your problem by [reading this](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – AnilPatel Apr 24 '13 at 12:56
3 Answers
One of the most common errors that I found developing Android Apps is the “java.lang.OutOfMemoryError: Bitmap Size Exceeds VM Budget” error. I found this error frecuently on activities using lots of bitmaps after changing orientation: the Activity is destroyed, created again and the layouts are “inflated” from the XML consuming the VM memory avaiable for bitmaps.
Bitmaps on the previous activity layout are not properly deallocated by the garbage collector because they have crossed references to their activity. After many experiments I found a quite good solution for this problem.
First, set the “id” attribute on the parent view of your XML layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/RootView"
>
...
Then, on the onDestroy() method of your Activity, call the unbindDrawables() method passing a refence to the parent View and then do a System.gc()
@Override
protected void onDestroy() {
super.onDestroy();
unbindDrawables(findViewById(R.id.RootView));
System.gc();
}
private void 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();
}
}
This unbindDrawables() method explores the view tree recursively and:
Removes callbacks on all the background drawables
Removes childs on every viewgroup
This solution works for me
- 6,287
- 2
- 51
- 90
-
1my heap grows like this: Grow heap (frag case) to 22.651MB for 4096016-byte allocation Grow heap (frag case) to 30.651MB for 4096016-byte allocation – User42590 Sep 28 '12 at 12:49
-
1Grow heap (frag case) to 35.651MB for 4096016-byte allocation and the 40 to 48. on flipping to last layout i get OOM error. due to this my activity crashes on second last layout. – User42590 Sep 28 '12 at 12:50
-
1on flipping to last layout i always get Out of memory on a 4096016-byte allocation. – User42590 Sep 28 '12 at 12:54
-
@AndroidUser I found I needed to add additional handling for `ImageView` elements: `if (view instanceof ImageView) { ((ImageView) view).setImageBitmap(null); ((ImageView) view).setImageDrawable(null); }` I can't figure out how to get a comment to format the code correctly, see my answer. – Andrew Aug 15 '14 at 01:40
I suggest to use the system.gc() and finish(); every time jump the another activity it's clear your used resources and free it. ok then does not occurred the out of memory error in your app. it's work try this.
Best of Luck
Use this link and get better solution
What Android tools and methods work best to find memory/resource leaks?
- 1
- 1
- 2,909
- 5
- 30
- 58
-
There should never be a need to call system.gc(). It's quite possible that he's holding onto references of the activities he's creating and thus Android can't remove them from memory. – ajacian81 Sep 28 '12 at 11:17
-
ok dear then use this link it`s very helpful to me i hope it help to you http://stackoverflow.com/questions/1147172/what-android-tools-and-methods-work-best-to-find-memory-resource-leaks/1147344#1147344 – Zala Janaksinh Sep 28 '12 at 11:24
-
-
I've been butting up against this issue on a Gingerbread (2.3.7) device with not a great deal of RAM (HTC Wildfire). After rotating the device a few times, the app crashes with:
E/AndroidRuntime( 4805): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
E/AndroidRuntime( 4805): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
E/AndroidRuntime( 4805): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
E/AndroidRuntime( 4805): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
E/AndroidRuntime( 4805): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
E/AndroidRuntime( 4805): at android.content.res.Resources.loadDrawable(Resources.java:1785)
E/AndroidRuntime( 4805): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
E/AndroidRuntime( 4805): at android.widget.ImageView.<init>(ImageView.java:118)
E/AndroidRuntime( 4805): at android.widget.ImageView.<init>(ImageView.java:108)
E/AndroidRuntime( 4805): ... 26 more
The layout that's rotating is pretty simple, a RelativeLayout with a few ImageView children, one of which has a relatively large (filesize) image as the background for part of the screen.
I tried implementing @AndroidUsers's solution but that did not correct my issue. With a bit more work I found that the ImageView I was holding the background image in wasn't releasing its memory properly so I added another check to the unbindDrawables method. I also bundled in the system.gc() call via an extra parameter so it can't be missed:
public static void unbindDrawables(View view, boolean runGC) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ImageView) {
((ImageView) view).setImageBitmap(null);
((ImageView) view).setImageDrawable(null);
}
else if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i), false);
}
((ViewGroup) view).removeAllViews();
}
if(runGC) {
System.gc();
}
}
In my testing it doesn't seem to matter which of the Bitmap or Drawable you set to null, but I figured both couldn't hurt.
- 1,037
- 11
- 19