安卓:内存不足错误 [英] Android: Out of memory error

查看:184
本文介绍了安卓:内存不足错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尽量减少我的Andr​​oid应用程序约4〜5次,我总是得到以下错误:

When I minimize my Android App for about 4 or 5 times, I always get the following error:

02-01 19:24:11.980: E/dalvikvm-heap(22362): Out of memory on a 3686416-byte allocation.
02-01 19:24:12.000: E/dalvikvm(22362): Out of memory: Heap Size=62755KB, Allocated=55237KB, Limit=65536KB
02-01 19:24:12.000: E/dalvikvm(22362): Extra info: Footprint=62435KB, Allowed Footprint=62755KB, Trimmed=2144KB
02-01 19:24:12.000: E/Bitmap_JNI(22362): Create Bitmap Failed.    
02-01 19:24:12.000: E/Bitmap_JNI(22362): Failed to create SkBitmap!
02-01 19:24:12.000: E/AndroidRuntime(22362): FATAL EXCEPTION: main
02-01 19:24:12.000: E/AndroidRuntime(22362): java.lang.OutOfMemoryError: (Heap Size=62755KB, Allocated=55237KB)
02-01 19:24:12.000: E/AndroidRuntime(22362):    at android.graphics.Bitmap.nativeCreateScaledBitmap(Native Method)
02-01 19:24:12.000: E/AndroidRuntime(22362):    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:744)
02-01 19:24:12.000: E/AndroidRuntime(22362):    at de.vauge.mb.Utils.getResizedBitmap(Utils.java:56)
02-01 19:24:12.000: E/AndroidRuntime(22362):    at de.vauge.mb.MenuView.initialize(MenuView.java:74)
02-01 19:24:12.000: E/AndroidRuntime(22362):    at de.vauge.mb.MenuView$1.handleMessage(MenuView.java:137)
02-01 19:24:12.000: E/AndroidRuntime(22362):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 19:24:12.000: E/AndroidRuntime(22362):    at android.os.Looper.loop(Looper.java:156)
02-01 19:24:12.000: E/AndroidRuntime(22362):    at android.app.ActivityThread.main(ActivityThread.java:5045)
02-01 19:24:12.000: E/AndroidRuntime(22362):    at java.lang.reflect.Method.invokeNative(Native Method)
02-01 19:24:12.000: E/AndroidRuntime(22362):    at java.lang.reflect.Method.invoke(Method.java:511)
02-01 19:24:12.000: E/AndroidRuntime(22362):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-01 19:24:12.000: E/AndroidRuntime(22362):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-01 19:24:12.000: E/AndroidRuntime(22362):    at dalvik.system.NativeStart.main(Native Method)

我的应用程序包括有7种不同的,自我点击数(所有的人都含有一些位图)它只是一个活动,他们被切换无形不需要(可能不是好作风,当他们,但它工作我到现在...)。这些观点的每个人都有的destroy() - 函数,回收所有在它使用的位图,并且在OnDestroy(在MainActivity的)呼吁所有的destroy() - 功能。此外,我没有使用任何静态位图。

My App consists of only one Activity which has 7 different, self-written Views (all of them containing some Bitmaps) in it, and they are toggled invisible when they are not needed (Probably not good style, but it worked for me until now...). Every one of those Views has a destroy()-function that recycles all the Bitmaps that are used in it, and the onDestroy() of the MainActivity calls all those destroy()-functions. Furthermore, I did not use any static Bitmaps.

所以,有什么事我可以尝试除了回收所有的位图,而不是使用静态位图?

So, is there anything else I could try in addition to recycling all Bitmaps and not using static Bitmaps?

推荐答案

嘛。 Android上的位图可以是一个有点棘手。你可以给有关的位图和它们的大小来源更好的信息?

Well. Bitmaps on Android can be a little tricky. Can you give better information about the sources of the bitmaps and their sizes?

另外,我建议你寻找到它上了几级:

Otherwise, I'd recommend looking into it on a few levels:

1。查看<一个href=\"http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inPurgeable\"相对=nofollow> inPurgeable 标志。它也被推荐的<一href=\"https://www.facebook.com/notes/facebook-engineering/under-the-hood-rebuilding-facebook-for-android/10151189598933920\"相对=nofollow> Facebook的工程博客

1. Check out inPurgeable flag. It has also been recommended by the facebook engineering blog


  1. 如果你正在装载远程图像, ImageLoader的库是一个公平的一块code的退房,也可以用来处理OOM。您还可以查看毕加索

  1. If you're loading remote images, the ImageLoader Library is a fair piece of code to check out, which also handles oom. You can also check out Picasso.

如果您使用的是以前是一个推荐的选项,试图找到办法解决它,因为它实际上会为每个图像要分配更多的内存inPurgable标志。

If you're using the inPurgable flag that used to be a recommended option, try to find a way around it as it actually causes more memory to be allocated for each image.

如果你去code小,本地资产频繁,考虑保存自己的可绘制在一个HashMap并在需要时再取它们。少GC。

If you decode small, local assets frequently, consider saving your drawables in a hashmap and fetching them again when needed. Less GC.

如果你关心你的子类应用程序,您可以使用的 OnLowMemory 打电话,知道什么时候你可能的真正的需要清理(主要是适合调试,并不是真实的生活情况)......如果这不是太晚了? :)

If you care to subclass your Application, you can use the OnLowMemory call to know when you probably really need to clean up (mostly good for debugging, not real life situations) ... If that's not too late... :)

看看克里斯·巴内斯的博客。 是一个pretty有趣内存缓存解决方案

Take a look at Chris Banes' blog. This is a pretty interesting memory cache solution

实施调用内存微调时即可

Implement a memory trimmer you call when you can

另外一个令人吃惊的优化是使用更小的物体时,你可以......想想你最小的数据模型和图像尺寸,并尝试对那些符合标准的API。

One other unsurprising optimization is to use smaller objects when you can... Think of your minimal data models and image sizes and try to have a conforming API for those.

这篇关于安卓:内存不足错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆