0

In my app, I have 7 radio buttons, each will take you to a specific activity. So in totality, I have 8 activities (home and 7 inner activities).

Each of the 7 inner activities, has a ViewFlipper, which contains 5 to 8 views.

When I run this app on device with OS version-2.2 or on device with OS version-2.3.3, it's working fine but when I ran the same app on device with OS version-4.0, it throws java.lang.OutOfMemoryError, infrequently (anywhere, for e.g. 3rd screen of 2nd activity's ViewFlipper).

This link on SO suggests to increase "Max VM application heap size", but since I'm testing on device, it's not possible.

This other link suggests to sample the bitmaps. I do use 15-20 images per activity(1 as background for each of the 5-8 screens and some additional images), but I set all the images in respective xml files only. I don't do anything to bitmaps in code.

At one place, I also found to use unbindDrawables() which I have place in onDestroy() of each activity, but to no avail.

Moreover, if memory is the real issue, then how come it works on 2.2 and 2.3.3 and doesn't work on 4.0 device which has updated OS and overall better configuration.

Is there some bug in OS version 4.0 related to this?

Can anyone guide me please?

Edit(1)

Logcat

     android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
    at android.view.LayoutInflater.createView(LayoutInflater.java:606)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:823)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:729)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
    at android.app.Activity.setContentView(Activity.java:1835)
    at MYACTIVITYNAME.onCreate(MemberActivity.java:167)
    at android.app.Activity.performCreate(Activity.java:4465)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    at android.app.ActivityThread.access$600(ActivityThread.java:123)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)


 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
    at android.view.LayoutInflater.createView(LayoutInflater.java:586)
    ... 27 more


 Caused by: java.lang.OutOfMemoryError
    at android.graphics.Bitmap.nativeCreate(Native Method)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
    at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
    at android.content.res.Resources.loadDrawable(Resources.java:1935)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
    at android.widget.ImageView.<init>(ImageView.java:119)
    at android.widget.ImageView.<init>(ImageView.java:109)
    ... 30 more
Community
  • 1
  • 1
GAMA
  • 5,958
  • 14
  • 79
  • 126
  • Post your full Logcat here. – hemantsb Sep 20 '13 at 11:29
  • I had a similar problem a while back, I don't think it is so much the OS version, as it is the phones with higher OS version tend to have higher resolution and thus eat up more memory (even more so if you are doing any image scaling in memory). – Kevin DiTraglia Sep 20 '13 at 11:29
  • @bangrang : added the full logcat. Please have a look. – GAMA Sep 20 '13 at 11:39
  • @KevinDiTraglia : So, what do you suggest in my case? – GAMA Sep 20 '13 at 11:40
  • I fixed it by disposing of any images that weren't on screen, and then reloading them if I needed them again. If that doesn't work you'll probably just need to compress your images down to smaller size, and just make sure you aren't doing lots of expensive operations in memory anywhere. It's definitely a tricky problem to fix. – Kevin DiTraglia Sep 20 '13 at 11:46
  • Use MAT to determine if you are leaking memory. – CommonsWare Sep 20 '13 at 11:48
  • @KevinDiTraglia : I have 7 activities. Each activity have a `ViewFlipper` which contains 5-8 screens. For particular activity, I need to switch between screens frequently, so disposing won't work as far as I understand it. If I'm leaving any activity, I'm unbinding all drawables from each screen of viewflipper. – GAMA Sep 20 '13 at 11:51
  • http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object try this – hemantsb Sep 20 '13 at 11:53
  • @bangrang : I'm not setting bitmaps from code. I'm doing that in xml only by using either android:background or android:src. So how can I use solution specified in this link? – GAMA Sep 20 '13 at 11:59
  • You tried this on emulator? or on real devices? – hemantsb Sep 20 '13 at 12:01
  • All 3 of them on real devices. – GAMA Sep 20 '13 at 12:01

1 Answers1

0

You may need to recycle your Bitmaps

I don't know if this might help you

Create Bitmap using this code

pass IMAGE URI to the function

      private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
          BitmapFactory.Options o = new BitmapFactory.Options();
          o.inJustDecodeBounds = true;
          BitmapFactory.decodeStream(
                       getContentResolver().openInputStream(selectedImage), null, o);

          final int REQUIRED_SIZE = 100;

          int width_tmp = o.outWidth, height_tmp = o.outHeight;
          int scale = 1;
          while (true) {
                 if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
                       break;
                 }
                 width_tmp /= 2;
                 height_tmp /= 2;
                 scale *= 2;
          }

          BitmapFactory.Options o2 = new BitmapFactory.Options();
          o2.inSampleSize = scale;
          return BitmapFactory.decodeStream(
                       getContentResolver().openInputStream(selectedImage), null, o2);
   }
WISHY
  • 11,067
  • 25
  • 105
  • 197
  • I'm not setting bitmaps from code. I'm doing that in xml only by using either `android:background` or `android:src`. So how can I use this solution? – GAMA Sep 20 '13 at 11:57