3

I had freed every object, recycle all the bitmaps in onDestroy() method:

     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();
    }
}
@Override
protected void onDestroy()
{
    super.onDestroy();
    unbindDrawables(findViewById(R.id.RootView));
            bitmap.recycle();
            bitmap=null;
    System.gc();

}

I have done this coding in every class onDestroy() method. But still my logcat shows me the error like:

            FATAL EXCEPTION: main
            java.lang.RuntimeException: Unable to start activity ComponentInfo{}: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
             android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
             android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
             android.app.ActivityThread.access$600(ActivityThread.java:123)
             android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
             android.os.Handler.dispatchMessage(Handler.java:99)
             android.os.Looper.loop(Looper.java:137)
             android.app.ActivityThread.main(ActivityThread.java:4424)
             java.lang.reflect.Method.invokeNative(Native Method)
             java.lang.reflect.Method.invoke(Method.java:511)
        com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
             dalvik.system.NativeStart.main(Native Method)
           Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
           android.view.LayoutInflater.createView(LayoutInflater.java:606)
                com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
            android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
            android.view.LayoutInflater.inflate(LayoutInflater.java:466)
            android.view.LayoutInflater.inflate(LayoutInflater.java:396)
             android.view.LayoutInflater.inflate(LayoutInflater.java:352)
           com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
           android.app.Activity.setContentView(Activity.java:1835)
           android.app.Activity.performCreate(Activity.java:4465)
          android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
          android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
          ... 11 more
          Caused by: java.lang.reflect.InvocationTargetException
          java.lang.reflect.Constructor.constructNative(Native Method)
          java.lang.reflect.Constructor.newInstance(Constructor.java:417)
          android.view.LayoutInflater.createView(LayoutInflater.java:586)
        ... 23 more
              Caused by: java.lang.OutOfMemoryError
              android.graphics.Bitmap.nativeCreate(Native Method)
              android.graphics.Bitmap.createBitmap(Bitmap.java:605)
              android.graphics.Bitmap.createBitmap(Bitmap.java:551)
              android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
              android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
              android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
              android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
          android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
           android.content.res.Resources.loadDrawable(Resources.java:1935)
           android.content.res.TypedArray.getDrawable(TypedArray.java:601)
           android.view.View.<init>(View.java:2785)
            android.view.View.<init>(View.java:2722)
            android.view.ViewGroup.<init>(ViewGroup.java:379)
           android.widget.RelativeLayout.<init>(RelativeLayout.java:174)
          ... 26 more
Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Kanika
  • 10,648
  • 18
  • 61
  • 81
  • 2
    ,,,,,can u show the layout ,,which you are using....so that i can tell,,,whats the exact situation of the problem you are facing,,,,Thanks Tushar – Cool Jatt Sep 11 '12 at 10:42
  • 1
    Also how many and how large are the graphics in your layout? Is this using an emulator/eclipse or testing on a real device? – pjco Sep 11 '12 at 10:45
  • like I am using more than 28 layouts in this project and I am also unBindDrawable(Root id of the layout) of each layout at onDestroy() method – Kanika Sep 11 '12 at 10:45
  • @pjco: No I am testing it on real device only, It is running on some devices, but crashing on some devices also – Kanika Sep 11 '12 at 10:46
  • What devices? Typically you should not have to manually unbind the drawables. That's an old hack that never really worked properly anyways. It sounds like you just have too many graphics/too complex a layout. – pjco Sep 11 '12 at 10:49
  • Here I am fetching Image from server and displaying It on screens. Also I am fetching the Image in Bitmap and at the end, I am passing null value to Bitmap.. – Kanika Sep 11 '12 at 10:51
  • @pjco : Then what would be the appropriate way of unbind the drawables?? – Kanika Sep 11 '12 at 10:52
  • You let the OS handle the drawables and garbage collection. Unless you have some reason you NEED to unbind (very unlikely), this is better left to the Android OS/dalvik – pjco Sep 11 '12 at 10:58
  • Then how can I resolve this problem????? – Kanika Sep 11 '12 at 10:58
  • please check this link.It will help you. Thanks http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object – Furqi Sep 11 '12 at 11:21

1 Answers1

8

The title says it all -- you are trying to allocate 73 megabytes of heap at once, for one image. This alone exceeds the heap size available on even most modern devices -- 64 megabytes. You need to find a way to use a significant smaller image.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173