Android的 - 位图缓存需要大量的内存位图、缓存、内存、Android

2023-09-12 03:21:08 作者:苹果砸晕了牛顿

我是新来的所有的内存管理问题,所以有很多事情我不明白。 我试图缓存的图像在我的应用程序,但我有麻烦与它的内存消耗:

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:

所有的位图Chaching code是pretty的很多复制粘贴从这里开始: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

我调试的code和检查堆大小的DDMS视图在Eclipse中,并没有关于这些code线后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);

在德codeSampledBitmapFromResource的方法。

in the "decodeSampledBitmapFromResource" method.

图片为1024x800,75KB的jpg文件。据我已经看到在互联网上,这种存储的图像应该是走量大约是1024 * 800 * 4(每像素字节)= 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?

修改

我试着用脱codeFILE方法上@ ArshadParwez的回答以下建议。使用这种方法,BitmapFactory.de codeStream方法后的内存仅增加了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.

我注意到,德codeResource方法的过程中有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.

例如,如果图像文件中的绘制文件夹中(即 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 = 13107200字节。

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

如果你跑了一个 xxhdpi 设备(如HTC之一,银河S4)上这将是更加糟糕。

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

你能做些什么?要么把图像文件中正确的密度文件夹(绘制-xhdpi 绘制-xxhdpi ),或者把它放在绘制-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.inJustDe codeBounds = 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.

关于向下扩展,你可以使用 谷歌的方式 或 我的路 每个人都有自己的优点和缺点。

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

关于缓存有很多方法可以做到这一点。最常见的一种是LRU缓存。还有我最近创建(链接这里的替代或here)它允许你缓存更大量的图像和避免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.