不同设备内的Android OutOfMemory?

时间:2012-08-15 15:36:58

标签: android bitmap logcat

我有一个处理图片的应用程序,它适用于某些设备ex。 HTC ONE X和其他人没有。 GALAXY注意,列表是大IDK的问题exatcly因为它在某些工作,并且它不在其他人。它也不能在模拟器上使用默认的,但是它可以工作的小的

这是我得到的logcat

08-15 18:30:39.070: I/dalvikvm-heap(334): Clamp target GC heap from 25.723MB to 24.000MB
08-15 18:30:39.090: D/dalvikvm(334): GC_FOR_MALLOC freed <1K, 51% free 2672K/5379K, external 19015K/19657K, paused 38ms
08-15 18:30:39.370: D/dalvikvm(334): GC_EXTERNAL_ALLOC freed <1K, 51% free 2672K/5379K, external 19015K/19657K, paused 45ms
08-15 18:30:39.400: E/dalvikvm-heap(334): 810000-byte external allocation too large for this process.
08-15 18:30:39.510: I/dalvikvm-heap(334): Clamp target GC heap from 25.724MB to 24.000MB
08-15 18:30:39.510: E/GraphicsJNI(334): VM won't let us allocate 810000 bytes
08-15 18:30:39.510: D/dalvikvm(334): GC_FOR_MALLOC freed 0K, 51% free 2672K/5379K, external 19015K/19657K, paused 36ms
08-15 18:30:39.532: D/AndroidRuntime(334): Shutting down VM
08-15 18:30:39.532: W/dalvikvm(334): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-15 18:30:39.600: E/AndroidRuntime(334): FATAL EXCEPTION: main
08-15 18:30:39.600: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.com.android.editor/app.com.android.editor.Option}: android.view.InflateException: Binary XML file line #317: Error inflating class <unknown>
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.os.Looper.loop(Looper.java:123)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-15 18:30:39.600: E/AndroidRuntime(334):  at java.lang.reflect.Method.invokeNative(Native Method)
08-15 18:30:39.600: E/AndroidRuntime(334):  at java.lang.reflect.Method.invoke(Method.java:507)
08-15 18:30:39.600: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-15 18:30:39.600: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-15 18:30:39.600: E/AndroidRuntime(334):  at dalvik.system.NativeStart.main(Native Method)
08-15 18:30:39.600: E/AndroidRuntime(334): Caused by: android.view.InflateException: Binary XML file line #317: Error inflating class <unknown>
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.createView(LayoutInflater.java:518)
08-15 18:30:39.600: E/AndroidRuntime(334):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
08-15 18:30:39.600: E/AndroidRuntime(334):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.Activity.setContentView(Activity.java:1657)
08-15 18:30:39.600: E/AndroidRuntime(334):  at app.com.android.editor.Option.onCreate(Option.java:40)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-15 18:30:39.600: E/AndroidRuntime(334):  ... 11 more
08-15 18:30:39.600: E/AndroidRuntime(334): Caused by: java.lang.reflect.InvocationTargetException
08-15 18:30:39.600: E/AndroidRuntime(334):  at java.lang.reflect.Constructor.constructNative(Native Method)
08-15 18:30:39.600: E/AndroidRuntime(334):  at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.LayoutInflater.createView(LayoutInflater.java:505)
08-15 18:30:39.600: E/AndroidRuntime(334):  ... 25 more
08-15 18:30:39.600: E/AndroidRuntime(334): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.Bitmap.nativeCreate(Native Method)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.content.res.Resources.loadDrawable(Resources.java:1709)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.view.View.<init>(View.java:1951)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.widget.TextView.<init>(TextView.java:344)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.widget.Button.<init>(Button.java:108)
08-15 18:30:39.600: E/AndroidRuntime(334):  at android.widget.Button.<init>(Button.java:104)
08-15 18:30:39.600: E/AndroidRuntime(334):  ... 28 more
08-15 18:30:45.459: I/Process(334): Sending signal. PID: 334 SIG: 9

4 个答案:

答案 0 :(得分:4)

堆大小因设备而异。在应用程序具有较大堆大小的设备上,与应用程序具有较小堆大小的设备相比,您遇到的问题更少是完全合理的。如果您支持较旧/较低分辨率的设备,那么您的堆可能低至16MB。

答案 1 :(得分:2)

大多数android的空间有限。通常为16MB。我强烈建议您查看图像大小。还可以使用弱引用来确保图像在访问时仅使用内存。 (弱引用会强制收集的垃圾更快地清理它们。)

  • 我对你的应用程序只有你的logcat错误列表知之甚少,但是如果你有多个活动,那就杀掉那些你不需要的活动。可能看似明显的建议,也许不是,但它会释放空间。

答案 2 :(得分:2)

正如其他人所说,应用程序的堆大小是有限的。最小值为16MB,可以在设备之间进行验证(这是虚拟机的设置)。

您可以做些什么建议:

  1. 如果不需要全分辨率,您可以加载较小版本的图像。以下是如何操作:http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html
  2. 检查是否有足够的可用内存;如果内存不足,您可以立即释放当前不需要的图像,并在需要时再次加载它们。
  3. 以下是一些用于检查剩余可用内存量的代码:

    long free = Runtime.getRuntime().freeMemory();
    

答案 3 :(得分:1)

 Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget

那条线说明了一切。 位图占用了某些设备的所有内存(VM堆栈限制较高的设备)。您应该为这个应用程序寻找更好的内存管理,例如LruCache,解码质量较低的位图等。

修改 一些用户可能会建议不要使用WeakReference。 请查看此问题Does Android need to load a complete Bitmap from a file before sampling it down?以及此视频http://www.youtube.com/watch?v=gbQb1PVjfqM

来自平台创建者的直接解释/教导/技术,并说不使用WeakReference。