LruCache不工作工作、LruCache

2023-09-07 16:22:04 作者:人海一粒渣

    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                // The cache size will be measured in kilobytes rather than
                // number of items.
                return bitmap.getByteCount() / 1024;
            }
        };
    URL url = new URL("https://m.xsw88.com/allimgs/daicuo/20230907/6218.png.jpg");
    Bitmap bitmap = BitmapFactory.decodeStream((InputStream) url.getContent(), null, options);
    if(bitmap != null)
        Log.i("Success", "BITMAP IS NOT NULL");

    String key = "myKey";
    Log.i("Get is null", "putting myKey");
    mMemoryCache.put(key, bitmap);

    Bitmap newBitmap = mMemoryCache.get(key);
    if(newBitmap == null)
        Log.i("newBitmap", "is null");

您好,这里是一个code。我得到的位图从URL成功(日志称位图不为空,我可以很容易显示出来)。然后我试图把它变成LruCache并拿回来,但它返回null。 (日志说newBitmap为空)。哪里是我的错?请告诉我。Android的4.1.2缓存大小8192 KB。

Hello, here is a code. I get bitmap from URL successfully (Log says Bitmap is not null and I can display it easy). Then I am trying to put it into LruCache and get it back, but it return null. (Log says newBitmap is null). Where is my mistake? Please, tell me. Android 4.1.2 Cache size 8192 Kb.

推荐答案

如果它在磁盘1.19 MB,但〜9 MB的内存,这意味着,作为一个COM pressed JPEG文件,它的1.19 MB,一旦你提取成可以显示位图(uncom pressed),它会占用9 MB内存。如果是按照您code片段的网址提出了1920×1200像素的图像时,图像会占用1920×1200×4字节的内存(4个字节为每个像素从0重新present ARGB值256倍230万总像素= 9216000字节)。如果您使用的可用内存的1/8这个缓存,有可能/可能是9MB超过总内存空间,这样的位图永远不会使它进入高速缓存或立即驱逐。

If it is 1.19 MB on disk but ~ 9 MB in memory, that means that as a compressed JPEG file, it's 1.19 MB and once you extract that into a Bitmap (uncompressed) that can be displayed, it will take up 9 MB in memory. If it's a 1920 x 1200 pixel image as suggested by the url in your code snippet, the image will take up 1920 x 1200 x 4 bytes of memory (4 bytes for each pixel to represent ARGB values from 0 to 256 times 2.3 million total pixels = 9,216,000 bytes). If you're using 1/8 of your available memory for this cache, it's possible/likely that 9MB exceeds that total memory space so the Bitmap never makes it into the cache or is evicted immediately.

你可能会想下采样在解码时的形象,如果它是一个大的(使用 BitmapFactory.Options.inSampleSize 文件...很多的网站使用,如果你还不熟悉)。

You're probably going to want to downsample the image at decoding time if it's that large (using BitmapFactory.Options.inSampleSize...lot's of documentation on the web for using that if you're not already familiar).

另外,你使用Runtime.maxMemory来计算您的高速缓存大小。这意味着你的要求,整个虚拟机被允许使用的最大内存量。

Also, you're using Runtime.maxMemory to compute your cache size. This means you're requesting the maximum amount of memory that the whole VM is allowed to use.

http://developer.android.com/reference/java/lang/Runtime.html#maxMemory%28%29

更常见的方法是使用由ActivityManager.getMemoryClass()方法,还给你的价值。

The more common approach is the use the value given back to you by the ActivityManager.getMemoryClass() method.

下面是一个例子code片段,并参考在文档上的方法定义。

Here's an example code snippet and the method definition in the docs for reference.

    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    int memClassBytes = am.getMemoryClass() * 1024 * 1024;
    int cacheSize = memClassBytes / 8;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize)

http://developer.android.com/reference/android/app/ActivityManager.html#getMemoryClass%28%29