在Android的ImageDownloader类可能的错误:sHardBitmapCache当它应该是不是静态的? [英] Possible BUG in Android ImageDownloader class : sHardBitmapCache NOT static when it should be?

查看:194
本文介绍了在Android的ImageDownloader类可能的错误:sHardBitmapCache当它应该是不是静态的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想今天学习尽可能多的有关特别着重于性能的Andr​​oid开发,因为在Play商店许多应用程序不畅。我发现/被定向到许多文章/录像。

I have been trying to learn as much as possible about Android development with specific focus on performance since many apps in the Play store today are sluggish. I have found/been directed to many articles/videos.

有关图像缓存一个具体的文章是:<一href="http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html">http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html

One specific article about image caching is at: http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html

作者有$ C $在可用的C:<一href="http://$c$c.google.com/p/android-imagedownloader/source/browse/trunk/src/com/example/android/imagedownloader/ImageDownloader.java">http://$c$c.google.com/p/android-imagedownloader/source/browse/trunk/src/com/example/android/imagedownloader/ImageDownloader.java

The author has code available at: http://code.google.com/p/android-imagedownloader/source/browse/trunk/src/com/example/android/imagedownloader/ImageDownloader.java

其中谷歌似乎采取一个版本到并投入其样本班:<一href="http://developer.android.com/resources/samples/XmlAdapters/src/com/example/android/xmladapters/ImageDownloader.html">http://developer.android.com/resources/samples/XmlAdapters/src/com/example/android/xmladapters/ImageDownloader.html

Which Google seemed to take a version of into and put into their sample classes at: http://developer.android.com/resources/samples/XmlAdapters/src/com/example/android/xmladapters/ImageDownloader.html

在总体上是实心的,除了我认为是在缓存的一个缺陷。它采用的是软/硬缓存把/获取的东西放入硬盘缓存,因为Android系统重置软缓存经常。

In general it is solid, except for what I think is a flaw in the caching. It uses a soft / hard cache that puts / gets things into the hard cache because the Android system resets the soft cache quite often.

纵观code,虽然一开始不知道如果在硬盘的缓存会得到意外重置每一个父类被实例化时。

Looking at the code though, one starts to wonder if the hard cache would get accidentally reset every time the parent class is instantiated.

一软缓存:

    // Soft cache for bitmaps kicked out of hard cache
    private final static ConcurrentHashMap<String, SoftReference<Bitmap>> sSoftBitmapCache =
        new ConcurrentHashMap<String, SoftReference<Bitmap>>(HARD_CACHE_CAPACITY / 2);

现在来看看硬盘的缓存:

Now take a look at the hard cache:

    // Hard cache, with a fixed maximum capacity and a life duration
    private final HashMap<String, Bitmap> sHardBitmapCache =
        new LinkedHashMap<String, Bitmap>(HARD_CACHE_CAPACITY / 2, 0.75f, true) {
        @Override
        protected boolean removeEldestEntry(LinkedHashMap.Entry<String, Bitmap> eldest) {
            if (size() > HARD_CACHE_CAPACITY) {
                // Entries push-out of hard reference cache are transferred to soft reference cache
                sSoftBitmapCache.put(eldest.getKey(), new SoftReference<Bitmap>(eldest.getValue()));
                return true;
            } else
                return false;
        }
    };

硬盘缓存没有静态,而软缓存是静态的。因此,硬缓存实例,因此项目将被清除的类的实例的生命。

The hard cache is not static, while the soft cache is static. So the hard cache instance and therefore items get cleared with the life of the instance of the class.

我觉得这是真正的原因是,我发现我有一个ListView / ImageView的应用程序,被下载每次的形象,从来没有将其高速缓存。这一切都是异步完成,但每次还是打网。我的验证此通过把 Log.d()语句我的方法内击中网页时/频率,它被称为看到。

The reason I think this is true is that I noticed my application with a ListView/ImageView, was downloading the image every time and never caching it. It was all done asynchronously, but still hitting the net every time. I verified this by putting a Log.d() statement inside my method that hits the web and seeing when/how often it was called.

添加static关键字固定的问题和我的应用程序是更高性能的。

我不知道为什么是这样的情况下,如图中的示例中有我的适配器ImageDownloader类的一个实例:

I am not sure why this is the case as there is only one instance of the ImageDownloader class in my adapter as shown in the example:

private final ImageDownloader imageDownloader = new ImageDownloader();


问题


THE QUESTION

使用所有这一切说,任何人都经历这个???还是我疯狂的/错的莫名其妙的组合。我没有Java /安卓/ JVM / Dalvik的/的WeakReference / SoftReference的专家,但有些事情似乎有点过。我不知道为什么 sHardBitmapCache 不被做静态的,但是当我进行了更改我的应用程序停止打网络这么多的(保存数据的成本/电池排水/性能增强)

With all of that said, has anyone else experienced this??? Or am I a combination of crazy/wrong somehow. I am no Java/Android/JVM/Dalvik/WeakReference/SoftReference expert, but something seems a bit off. I don't know why sHardBitmapCache was not made static, but when I made the change my application stopped hitting the web so much (saving on data costs / battery drainage / performance improvements).

推荐答案

您是正确的,这是我的一部分错字。

You are correct, this is a typo on my part.

我有固定的code Android的源代码树研究。感谢这个反馈。

I have fixed the code in the android source tree. Thanks for this feedback.

这篇关于在Android的ImageDownloader类可能的错误:sHardBitmapCache当它应该是不是静态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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