Android:图库内存不足异常
我的应用程序显示 9 个类别的列表,每个类别显示基于图库的封面流程(由 Neil Davies 此处)包含所选类别的图像。
这些图像是从 Web 获取的,每个图像的大小从 300K 到 500K 不等,并存储在 Drawables 的数组列表中。该数据使用 BaseAdapter(代码如下)绑定到 coverflow。
每次我退出 coverflow 并返回到类别列表时,我都会清除 arrayList(同样,下面的代码)。
在场景 1 中,我的 arrayList 包含 5 个 Drawable。在这种情况下,我可以自由浏览所有类别并显示它们的图像。在我的测试过程中,我将所有类别循环了 5 次,这似乎足以确定没有问题。
在场景 2 中,我的 arrayList 包含 10 个可绘制对象。在这种情况下,我在浏览第五或第六类别内的图像时遇到 OutOfMemoryError 异常:
07-13 08:38:21.266: ERROR/dalvikvm-heap(2133): 819840-byte external allocation too large for this process. 07-13 08:38:21.266: ERROR/(2133): VM won't let us allocate 819840 bytes 07-13 08:38:21.277: DEBUG/skia(2133): --- decoder->decode returned false 07-13 08:38:21.287: WARN/dalvikvm(2133): threadid=25: thread exiting with uncaught exception (group=0x4001b188) 07-13 08:38:21.296: ERROR/AndroidRuntime(2133): Uncaught handler: thread Thread-64 exiting due to uncaught exception 07-13 08:38:21.308: ERROR/AndroidRuntime(2133): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 07-13 08:38:21.308: ERROR/AndroidRuntime(2133): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 07-13 08:38:21.308: ERROR/AndroidRuntime(2133): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459) 07-13 08:38:21.308: ERROR/AndroidRuntime(2133): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323) 07-13 08:38:21.308: ERROR/AndroidRuntime(2133): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) 07-13 08:38:21.308: ERROR/AndroidRuntime(2133): at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
这对我来说没有意义。如果我泄漏内存,我预计会在场景 1 中的某个时刻崩溃,但我多次检查了所有类别并且没有崩溃。我还使用了 Eclipse 的内存分析器插件,它没有出现任何潜在的罪魁祸首。
如果系统无法处理 10 个图像(如场景 2 中所示),我预计会在第一个类别中崩溃,但只有在 5 或 6 个类别后才会崩溃。
coverflow 的适配器功能:
public int getCount() {
return DataManager.getInstance().getImageBufferInstance().getImageArraySize();
}
public Object getItem(int position) {
return DataManager.getInstance().getImagesBuffer().get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i;
if (convertView == null)
i = new ImageView(mContext);
else
i = (ImageView)convertView;
Drawable bufferedImage = (Drawable)getItem(position);
Log.v("getView", "position: " + position);
i.setImageDrawable(bufferedImage);
i.setLayoutParams(new CoverFlow.LayoutParams(Utils.getInstance().getScreenWidth() / 2,
Utils.getInstance().getScreenHeight() / 2));
i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
try{
//Make sure we set anti-aliasing otherwise we get jaggies
BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
drawable.setAntiAlias(true);
}
catch (Exception e)
{
Log.v("getView", "Exception: " + e.toString());
}
return i;
}
进入类别时填充数据源:
for (int i = 0; i < ImageBuffer.getInstance().getImageArraySize(); i++)
{
String imageUrl = ImageBuffer.getInstance().getImageUrl(i);
Log.v("Initial", imageUrl);
Drawable fullImage = AsyncImageLoader.getInstance().loadImageByUrl(imageUrl);
ImageBuffer.getInstance().getImages().add(i, fullImage);
}
退出类别时清除数据源(在 finish() 中):
for (int i = 0; i < ImageBuffer.getInstance().getImageArraySize(); i++)
{
if (ImageBuffer.getInstance().images.get(i) != null)
{
ImageBuffer.getInstance().images.get(i).setCallback(null);
ImageBuffer.getInstance().images.set(i, null);
}
}
编辑:
好的,我在 coverflow 上应用了 Mathias 的 LogHeap 函数,这里是一些输出。 加载第一个图库之前:
DEBUG/Application(5221): debug. ================================= DEBUG/Application(5221): debug.heap native: allocated 6.20MB of 6.28MB (0.07MB free) in [com.example.Coverflow] DEBUG/Application(5221): debug.memory: allocated: 4.00MB of 24.00MB (0.00MB free) DEBUG/dalvikvm(5221): GC freed 4558 objects / 638152 bytes in 84ms DEBUG/dalvikvm(5221): GC freed 17 objects / 808 bytes in 67ms
进入第一个图库之后:
DEBUG/Application(5221): debug. ================================= DEBUG/Application(5221): debug.heap native: allocated 14.90MB of 16.89MB (0.07MB free) in [com.example.Coverflow] DEBUG/Application(5221): debug.memory: allocated: 4.00MB of 24.00MB (1.00MB free) DEBUG/dalvikvm(5221): GC freed 357 objects / 50080 bytes in 68ms DEBUG/dalvikvm(5221): GC freed 353 objects / 27312 bytes in 67ms
第一个图库存在之后:
DEBUG/Application(5221): debug. ================================= DEBUG/Application(5221): debug.heap native: allocated 14.83MB of 16.89MB (0.11MB free) in [com.example.Coverflow] DEBUG/Application(5221): debug.memory: allocated: 4.00MB of 24.00MB (1.00MB free) DEBUG/dalvikvm(5221): GC freed 330 objects / 17920 bytes in 77ms DEBUG/dalvikvm(5221): GC freed 13 objects / 760 bytes in 67ms
进入第五个图库之后:
DEBUG/Application(5221): debug. ================================= DEBUG/Application(5221): debug.heap native: allocated 16.80MB of 23.32MB (0.08MB free) in [com.example.Coverflow] DEBUG/Application(5221): debug.memory: allocated: 4.00MB of 24.00MB (1.00MB free) DEBUG/dalvikvm(5221): GC freed 842 objects / 99256 bytes in 73ms DEBUG/dalvikvm(5221): GC freed 306 objects / 24896 bytes in 69ms
退出第五个图库之后:
DEBUG/Application(5221): debug. ================================= DEBUG/Application(5221): debug.heap native: allocated 16.74MB of 23.32MB (0.11MB free) in [com.example.Coverlow] DEBUG/Application(5221): debug.memory: allocated: 4.00MB of 24.00MB (1.00MB free) DEBUG/dalvikvm(5221): GC freed 331 objects / 18184 bytes in 68ms DEBUG/dalvikvm(5221): GC freed 60 objects / 3128 bytes in 68ms
进入图库时似乎分配的内存越来越多,但释放的很少退出后。我没有正确清除我的绘图吗?对于可绘制对象的 arrayList 中的每个元素,我调用 setCallBack(null) 并将该元素设置为 null。这还不够吗?
迫切需要任何见解。
谢谢
My app shows a list of 9 categories and each category displays a Gallery-based coverflow (graciously offered by Neil Davies here) with images of the selected category.
The images are fetched from the Web, each ranging from 300K to 500K in size, and stored in an arrayList of Drawables. This data is bound to the coverflow using a BaseAdapter (code below).
Every time I exit the coverflow and go back to the list of categories, I clear the arrayList (again, code below).
In scenario 1, my arrayList contains 5 Drawables. In this scenario, I can freely browse all the categories and show their images. During my test I cycled through all the categories for 5 times, which seems enough to determine that there is no problem.
In scenario 2, my arrayList contains 10 drawables. In this scenario, I get an OutOfMemoryError exception while going through images inside the 5th or 6th categeory:
07-13 08:38:21.266: ERROR/dalvikvm-heap(2133): 819840-byte external allocation too large for this process. 07-13 08:38:21.266: ERROR/(2133): VM won't let us allocate 819840 bytes 07-13 08:38:21.277: DEBUG/skia(2133): --- decoder->decode returned false 07-13 08:38:21.287: WARN/dalvikvm(2133): threadid=25: thread exiting with uncaught exception (group=0x4001b188) 07-13 08:38:21.296: ERROR/AndroidRuntime(2133): Uncaught handler: thread Thread-64 exiting due to uncaught exception 07-13 08:38:21.308: ERROR/AndroidRuntime(2133): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 07-13 08:38:21.308: ERROR/AndroidRuntime(2133): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 07-13 08:38:21.308: ERROR/AndroidRuntime(2133): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459) 07-13 08:38:21.308: ERROR/AndroidRuntime(2133): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323) 07-13 08:38:21.308: ERROR/AndroidRuntime(2133): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) 07-13 08:38:21.308: ERROR/AndroidRuntime(2133): at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
This doesn't make sense to me. If I am leaking memory I would have expected to crash at some point in scenario 1, but I went through all the categories a substantial number of times and didn't crash. I also used the Memory Analyzer plugin for Eclipse which didn't present any potential culprits.
If the system couldn't handle 10 images, like in scenarion 2, I would have expected to crash in the first category, but I crash only after 5 or 6 categories.
The coverflow's adapter functions:
public int getCount() {
return DataManager.getInstance().getImageBufferInstance().getImageArraySize();
}
public Object getItem(int position) {
return DataManager.getInstance().getImagesBuffer().get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i;
if (convertView == null)
i = new ImageView(mContext);
else
i = (ImageView)convertView;
Drawable bufferedImage = (Drawable)getItem(position);
Log.v("getView", "position: " + position);
i.setImageDrawable(bufferedImage);
i.setLayoutParams(new CoverFlow.LayoutParams(Utils.getInstance().getScreenWidth() / 2,
Utils.getInstance().getScreenHeight() / 2));
i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
try{
//Make sure we set anti-aliasing otherwise we get jaggies
BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
drawable.setAntiAlias(true);
}
catch (Exception e)
{
Log.v("getView", "Exception: " + e.toString());
}
return i;
}
populating the data source upon entry to the category:
for (int i = 0; i < ImageBuffer.getInstance().getImageArraySize(); i++)
{
String imageUrl = ImageBuffer.getInstance().getImageUrl(i);
Log.v("Initial", imageUrl);
Drawable fullImage = AsyncImageLoader.getInstance().loadImageByUrl(imageUrl);
ImageBuffer.getInstance().getImages().add(i, fullImage);
}
clearing the data source when exiting the category (in finish()):
for (int i = 0; i < ImageBuffer.getInstance().getImageArraySize(); i++)
{
if (ImageBuffer.getInstance().images.get(i) != null)
{
ImageBuffer.getInstance().images.get(i).setCallback(null);
ImageBuffer.getInstance().images.set(i, null);
}
}
EDIT:
OK, I applied Mathias' LogHeap function on my coverflow and here are some outputs.
Prior to loading the first gallery:
DEBUG/Application(5221): debug. ================================= DEBUG/Application(5221): debug.heap native: allocated 6.20MB of 6.28MB (0.07MB free) in [com.example.Coverflow] DEBUG/Application(5221): debug.memory: allocated: 4.00MB of 24.00MB (0.00MB free) DEBUG/dalvikvm(5221): GC freed 4558 objects / 638152 bytes in 84ms DEBUG/dalvikvm(5221): GC freed 17 objects / 808 bytes in 67ms
After entering the first gallery:
DEBUG/Application(5221): debug. ================================= DEBUG/Application(5221): debug.heap native: allocated 14.90MB of 16.89MB (0.07MB free) in [com.example.Coverflow] DEBUG/Application(5221): debug.memory: allocated: 4.00MB of 24.00MB (1.00MB free) DEBUG/dalvikvm(5221): GC freed 357 objects / 50080 bytes in 68ms DEBUG/dalvikvm(5221): GC freed 353 objects / 27312 bytes in 67ms
After existing the first gallery:
DEBUG/Application(5221): debug. ================================= DEBUG/Application(5221): debug.heap native: allocated 14.83MB of 16.89MB (0.11MB free) in [com.example.Coverflow] DEBUG/Application(5221): debug.memory: allocated: 4.00MB of 24.00MB (1.00MB free) DEBUG/dalvikvm(5221): GC freed 330 objects / 17920 bytes in 77ms DEBUG/dalvikvm(5221): GC freed 13 objects / 760 bytes in 67ms
After entering the fifth gallery:
DEBUG/Application(5221): debug. ================================= DEBUG/Application(5221): debug.heap native: allocated 16.80MB of 23.32MB (0.08MB free) in [com.example.Coverflow] DEBUG/Application(5221): debug.memory: allocated: 4.00MB of 24.00MB (1.00MB free) DEBUG/dalvikvm(5221): GC freed 842 objects / 99256 bytes in 73ms DEBUG/dalvikvm(5221): GC freed 306 objects / 24896 bytes in 69ms
After exiting the fifth gallery:
DEBUG/Application(5221): debug. ================================= DEBUG/Application(5221): debug.heap native: allocated 16.74MB of 23.32MB (0.11MB free) in [com.example.Coverlow] DEBUG/Application(5221): debug.memory: allocated: 4.00MB of 24.00MB (1.00MB free) DEBUG/dalvikvm(5221): GC freed 331 objects / 18184 bytes in 68ms DEBUG/dalvikvm(5221): GC freed 60 objects / 3128 bytes in 68ms
It seems that more and more memory is allocated when entering a gallery, but very little is released after exiting. Am I not clearing my drawables properly? For each element in my arrayList of drawables I call setCallBack(null) and set the element to null. Is that not enough?
Desperate for any insight.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您从网络加载的图像的 kb 文件大小并不直接相关。由于它们被转换为位图,因此您需要为常规 ARGB 图像计算每个图像的宽度 * 高度 * 4 字节。 (宽度和高度以 px 为单位)。
位图消耗本机堆,这通常不会显示在 hprof 中。 hprof 应该只显示对象的数量,即剩余的 BitmapDrawables 或 Bitmaps。
我在应用程序中使用此代码来输出应用程序和本机堆当前使用的内存:
在开发过程中开始或完成活动时我会调用它。
这里有一些信息链接 - 一般来说这里有很多关于这个主题的线程。
这里还有一张有用的幻灯片作者:Romain Guy(Android 框架工程师),关于软引用、弱引用、简单缓存、图像处理:
http://docs.huihoo.com/google/io/2009/ Th_0230_TurboChargeYourUI-如何使你的AndroidUI快速而高效.pdf
The kb file size of the image you're loading from the web isn't directly relevant. Since they're converted into bitmaps you need to calculate width * height * 4 bytes per image for regular ARGB images. (width and height in px).
The bitmaps consume native heap, which usually doesn't show in a hprof. The hprof should only show you the number of objects, i.e. BitmapDrawables or Bitmaps that are left.
I use this code in my app to output the current used memory used by the app and native heap:
which I call when starting or finishing an activity during development.
Here are some informative links - generally there are lots of threads about this topic on here.
Here's also a useful slide by Romain Guy (Android Framework engineer) about soft references, weak references, simple caches, image handling:
http://docs.huihoo.com/google/io/2009/Th_0230_TurboChargeYourUI-HowtomakeyourAndroidUIfastandefficient.pdf
以下是一些建议:
您使用 inSampleSize 选项吗?如果缩放图像,它会减少内存消耗。 将图像加载到 Bitmap 对象时出现奇怪的内存不足问题
当你不再需要图像时,你应该调用 Bitmap.recycle()。我认为这对你的情况很重要。 Android: OutofMemoryError: 位图大小超出了 VM 预算,我看不出任何原因
Here are some advices:
Do you use inSampleSize option? It reduces memory consumption if you scale images. Strange out of memory issue while loading an image to a Bitmap object
You should call Bitmap.recycle() when you don't need images any more. I think it's important in your case. Android: OutofMemoryError: bitmap size exceeds VM budget with no reason I can see
您在图库 5 或 6 中加载的图像可能太大而无法加载,并且超出了虚拟机允许的最大大小。
The image you are loading in gallery 5 or 6 could be too large to load and it is exceeding the maximum size allow by the VM.
您最好清楚,
getView
的参数列表中的convertView
始终为null
。也就是说,gallery并没有复用里面的旧视图。You'd better be well aware that the
convertView
in parameters list ofgetView
is alwaysnull
. That is to say, gallery does not reuse the old view inside.