Tiny Images, No Rotation, but still get OutOfMemoryError: bitmap size exceeds VM budget

可紊 提交于 2020-02-04 11:42:06

问题


I've spent the morning reading through page after page of StackOverflow posts about this exact error, but the root cause of their problems always seems to be either 1) they have very large images which need to be down sampled or 2) they are suffering after one or more device rotations which cause the activity to be destroyed and recreated several times.

I'm getting this error (only very rarely), but my app only allows portrait orientation so no rotations are possible and all of my images are very small. I do have many images, but they are all very small. It is a calculator app, so there are images for each button. The images are only 1.9k for the mdpi and 2.8k for the hdpi. These are tiny, tiny images. I processed them through Photoshop to downsample all of them to 8bit PNGs. Here is an example of one of my images: http://geekqi.com/StackOverflow/OutOfMemoryImage.png

There are about 80 of these ImageButtons (only 40 of which are on the screen at once). I have two views, each with 40 ImageButtons and when you press a button, one of the views is hidden and the other shown (offering a Shift functionality to allow access to more functions). Each button has a standard image and a highlighted image. So, all in all I'm dealing with 160 tiny images. The biggest one is 2.8k so 160 of them would be 448k... even if the ImageButton overhead would double that size, it's less than 1MB.

Below is the stacktrace I'm receiving. It references line 296, which looks like:

<ImageButton android:id="@+id/buttonRND" android:src="@drawable/image_rnd" android:hapticFeedbackEnabled="true" style="@style/CalculatorButton"/>

Here is the stack trace I'm dealing with:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain.myapp/com.mydomain.myapp.CalculatorTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain.myapp/com.mydomain.myapp.MyCalculator}: android.view.InflateException: Binary XML file line #296: Error inflating class <unknown>
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
       at android.app.ActivityThread.access$2300(ActivityThread.java:125)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:123)
       at android.app.ActivityThread.main(ActivityThread.java:4627)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:521)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
       at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain.myapp/com.mydomain.myapp.MyCalculator}: android.view.InflateException: Binary XML file line #296: Error inflating class <unknown>
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
       at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
       at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
       at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
       at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
       at android.widget.TabHost.setCurrentTab(TabHost.java:323)
       at android.widget.TabHost.addTab(TabHost.java:213)
       at com.mydomain.myapp.CalculatorTabActivity.setupTab(CalculatorTabActivity.java:191)
       at com.mydomain.myapp.CalculatorTabActivity.onCreate(CalculatorTabActivity.java:95)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
       ... 11 more
Caused by: android.view.InflateException: Binary XML file line #296: Error inflating class <unknown>
       at android.view.LayoutInflater.createView(LayoutInflater.java:513)
       at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
       at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
       at android.app.Activity.setContentView(Activity.java:1647)
       at com.mydomain.myapp.MyCalculator.configureActivity(MyCalculator.java:1042)
       at com.mydomain.myapp.MyCalculator.onCreate(MyCalculator.java:1111)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
       ... 21 more
Caused by: java.lang.reflect.InvocationTargetException
       at android.widget.ImageButton.<init>(ImageButton.java:78)
       at java.lang.reflect.Constructor.constructNative(Native Method)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
       at android.view.LayoutInflater.createView(LayoutInflater.java:500)
       ... 36 more
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
       at android.graphics.Bitmap.nativeCreate(Native Method)
       at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
       at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
       at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
       at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:590)
       at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:564)
       at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:425)
       at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
       at android.content.res.Resources.loadDrawable(Resources.java:1709)
       at android.content.res.Resources.getDrawable(Resources.java:581)
       at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
       at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
       at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
       at android.content.res.Resources.loadDrawable(Resources.java:1694)
       at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
       at android.widget.ImageView.<init>(ImageView.java:118)
       at android.widget.ImageButton.<init>(ImageButton.java:82)
       ... 40 more

I've read through all of the links about tracking down Android memory "leaks", but they also deal with rotation and my app doesn't allow rotation.

What can I do to find and fix this problem?


回答1:


You don't need to rotate app to get Activity restarted. It is enough to start another activity and then start the original one.

Do you keep references to your images inside your activity? How do you keep them? In fields? Are you referencing image drawables?




回答2:


This looks like a horrible problem I had a few months ago.

Do you have layers of transparent PNG images / backgrounds somewhere in your application (not limited to the activity you're in when the crash happens)?

What I was trying to do was minimize file size by saving image layers individually and just putting them together in the app (since I'd use some layers on their own elsewhere.)

This was producing a stack trace similar to what you're describing.

Hope this helps.



来源:https://stackoverflow.com/questions/5206784/tiny-images-no-rotation-but-still-get-outofmemoryerror-bitmap-size-exceeds-vm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!