Android - 位图缓存占用大量内存 [英] Android - Bitmap cache takes a lot of memory

查看:29
本文介绍了Android - 位图缓存占用大量内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是所有内存管理主题的新手,所以有很多我不明白的东西.
我正在尝试在我的应用程序中缓存一个图像,但我遇到了它的内存消耗问题:

I'm new to all the memory management subject, so there are a lot of things I don't understand.
I'm trying to cache an image in my app, but I'm having troubles with its memory consumption:

所有 Bitmap Chaching 代码几乎都是从这里复制粘贴的:http://developer.android.com/training/displaying-bitmaps/index.html

All of the Bitmap Chaching code is pretty much copy-pasted from here: http://developer.android.com/training/displaying-bitmaps/index.html

我在eclipse的DDMS视图中调试了代码并检查了heap size,在这些代码行之后有大约15mb的跳转:

I debugged the code and checked the heap size in the DDMS view in eclipse, and there is about 15mb jump after these code lines:

        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);

在decodeSampledBitmapFromResource"方法中.

in the "decodeSampledBitmapFromResource" method.

图像为 1024x800、75kb 的 jpg 文件.根据我在互联网上已经看到的,这张图片应该占用的内存量大约是 1024*800*4(Bytes per pixel)=3.125mb

The image is 1024x800, 75kb jpg file. According to what I've already seen on the internet, the amount of memory this image is supposed to take is about 1024*800*4(Bytes per pixel)=3.125mb

关于这个主题的所有线程都没有说明为什么它需要比它应该占用更多的内存.有没有办法用合理的内存量缓存一张图片?

All of the threads regarding this subject don't say why it's taking much more memory than it should. Is there a way to cache one image with a reasonable amount of memory?

编辑

我尝试使用下面@ArshadParwez 的回答中建议的 decodeFile 方法.使用这种方法,在 BitmapFactory.decodeStream 方法之后,内存仅增加了 3.5mb - 问题解决了,但我想直接从资源中缓存位图.

I tried using the decodeFile method suggested on @ArshadParwez's answer below. Using this method, after the BitmapFactory.decodeStream method the memory is increased by only 3.5mb - problem solved, sort of, but I want to cache bitmaps directly from the resource.

我注意到在 decodeResource 方法期间有 2 次内存跳跃" - 一次大约 3.5mb - 这是合理的,另一个是 14mb 的奇怪.那些 14mb 是做什么用的,为什么会发生这种情况?

I noticed that during the decodeResource method there are 2 memory "jumps" - one of about 3.5mb - which is reasonable, and another strange one of 14mb. What are those 14mb used for and why does this happen?

推荐答案

图像也会根据密度进行缩放,因此它们可以使用大量内存.

Images are also scaled according to the density so they can use a lot of memory.

例如,如果图像文件位于 drawable 文件夹中(即 mdpi 密度)并且您在 xhdpi 设备上运行它,宽度和高度都会加倍.也许这个链接可以帮助你,或这个.

For example, if the image file is in the drawable folder (which is mdpi density) and you run it on an xhdpi device, both the width and the height would double. Maybe this link could help you, or this one.

因此在您的示例中,图像文件将占用的字节是:

So in your example the bytes the image file would take are :

(1024*2)*(800*2)*4 = 13,107,200 字节.

(1024*2)*(800*2)*4 = 13,107,200 bytes.

如果您在 xxhdpi 设备(如 HTC one 和 Galaxy S4)上运行它会更糟.

It would be even worse if you ran it on an xxhdpi device (like the HTC one and Galaxy S4) .

你能做什么?将图像文件放入正确的密度文件夹(drawable-xhdpidrawable-xxhdpi)或将其放入 drawable-nodpi(或资产文件夹)并根据您的需要缩小图像.

What can you do? Either put the image file in the correct density folder (drawable-xhdpi or drawable-xxhdpi) or put it in drawable-nodpi (or in the assets folder) and downscale the image according to your needs.

顺便说一句,您不必设置 options.inJustDecodeBounds = false 因为这是默认行为.事实上,您可以为位图选项设置 null.

BTW you don't have to set options.inJustDecodeBounds = false since it's the default behavior. In fact you can set null for the bitmap options.

关于缩小规模,您可以使用 google 的方式我的方式各有优缺点.

About down scaling you can use either google's way or my way each has its own advantages and disadvantages.

关于缓存,有很多方法可以做到.最常见的一种是 LRU 缓存.我最近还创建了一个替代方案(链接 此处此处) 允许您缓存更多图像和避免出现 OOM,但它会给您带来很多责任.

About caching there are many ways to do it. The most common one is LRU cache. There is also an alternative I've created recently (link here or here) that allows you to cache a lot more images and avoid having OOM but it gives you a lot of responsibility.

这篇关于Android - 位图缓存占用大量内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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