开发者

Android OOM For Bitmap with image Capture

开发者 https://www.devze.com 2023-02-11 20:11 出处:网络
I have been battling this \"memory leak\" for the past 3 days non-stop and while I have streamlined other parts of my application this problem has remained.

I have been battling this "memory leak" for the past 3 days non-stop and while I have streamlined other parts of my application this problem has remained.

First what I am trying to do. I am firing an Intent for IMAGE_CAPTURE with EXTRA_OUTPUT to save the image to a file. Then I display the returned image scaled down by an order of 2, for the user, letting him select a square portion of the image. Once he selects it, he is taken to another Activity that lets him tag the image with some text and such, and eventually upload everything using HTTPPost to the server.

Right now, everything works great exactly once! I can go through it and everything works, without any problem. If I try to take another picture however, I always get an Out of Memory Exception. Here is the Logcat for the latest time it's happened

02-18 19:07:19.498: ERROR/dalvikvm-heap(6385): 1040400-byte external allocation too large for this process.
02-18 19:07:19.498: ERROR/GraphicsJNI(6385): VM won't let us allocate 1040400 bytes
02-18 19:07:19.498: DEBUG/AndroidRuntime(6385): Shutting down VM
02-18 19:07:19.498: WARN/dalvikvm(6385): threadid=1: thread exiting with uncaught exception (group=0x4001d7f0)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385): FATAL EXCEPTION: main
02-18 19:07:19.518: ERROR/AndroidRuntime(6385): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hitpost/com.hitpost.SharePicture}: android.view.InflateException: Binary XML file line #29: Error inflating class <unknown>
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.os.Looper.loop(Looper.java:123)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at java.lang.reflect.Method.invoke(Method.java:521)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at dalvik.system.NativeStart.main(Native Method)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385): Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class <unknown>
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.app.Activity.setContentView(Activity.java:1647)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at com.hitpost.SharePicture.onCreate(SharePicture.java:90)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     ... 11 more
02-18 19:07:19.518: ERROR/AndroidRuntime(6385): Caused by: java.lang.reflect.InvocationTargetException
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.widget.ImageView.<init>(ImageView.java:108)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at java.lang.reflect.Constructor.constructNative(Native Method)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     ... 23 more
02-18 19:07:19.518: ERROR/AndroidRuntime(6385): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.graphics.Bitmap.nativeCreate(Native Method)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.content.res.Resources.loadDrawable(Resources.java:1709)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     at android.widget.ImageView.<init>(ImageView.java:118)
02-18 19:07:19.518: ERROR/AndroidRuntime(6385):     ... 27 more

It is failing on the setContentView of the last Activity, as in it displays the picture once to the user, but is somehow not able to display it again.

I would really appreciate the help!

Updated I have been testing this issue on multiple phones, and the only one where it seems to be doing this is the Nexus One with Froyo. Works on Samsung Captivate 2.1, doesn't crash in regard to this issue on Moto Milestone 2.1.

Also I need a 500 x 500 image at the end so I can't scal开发者_运维问答e down by a factor more than 2.


See my response at BitmapFactory OOM driving me nuts.

Your bitmap data is in the Native heap, not the VM heap. So explicit VM garbage collection will have no effect.

The native heap is garbage collected but less frequently / aggressively. The only approach we have found to work is

  • monitor the Native heap (as per the link above) to make sure you do not hit the OOM exception
  • free up bitmaps when you do not need them (which will reclaim space in the Native heap) by doing (as you are doing)

    mBitmap.recycle(); mBitmap = null;


You should try wrapping the Bitmap objects in a SoftReference object, so that it gets reclaimed by the OS when memory is low and when your app is not using the image in the current activity. Also you should think about relinquishing the memory occupied by the image when your activity changes, and you should reload the image again from the file when your activity with that particular image is resumed.

0

精彩评论

暂无评论...
验证码 换一张
取 消