[Android] Use references in Cache
Geoffrey Métais
git at videolan.org
Wed Jun 7 13:55:52 CEST 2017
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Jun 7 13:40:31 2017 +0200| [1c6ff58823cc4d3f9c7acaac2a4877f1e467a479] | committer: Geoffrey Métais
Use references in Cache
SoftReferences make in inefficient
> https://code.videolan.org/videolan/vlc-android/commit/1c6ff58823cc4d3f9c7acaac2a4877f1e467a479
---
.../org/videolan/vlc/gui/helpers/BitmapCache.java | 54 ++++------------------
1 file changed, 10 insertions(+), 44 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/BitmapCache.java b/vlc-android/src/org/videolan/vlc/gui/helpers/BitmapCache.java
index b8eb058f5..e4a5baeb1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/BitmapCache.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/BitmapCache.java
@@ -31,20 +31,15 @@ import android.support.v4.util.LruCache;
import android.util.Log;
import org.videolan.libvlc.util.AndroidUtil;
-import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
-
-import java.lang.ref.SoftReference;
+import org.videolan.vlc.util.Strings;
public class BitmapCache {
- public final static String TAG = "VLC/BitmapCache";
- private final static boolean LOG_ENABLED = false;
+ private final static String TAG = "VLC/BitmapCache";
- private static final String CONE_KEY = "res:"+ R.drawable.cone;
- private static final String CONE_O_KEY = "res:"+ R.drawable.ic_cone_o;
private static BitmapCache mInstance;
- private final LruCache<String, CacheableBitmap> mMemCache;
+ private final LruCache<String, Bitmap> mMemCache;
public synchronized static BitmapCache getInstance() {
if (mInstance == null)
@@ -64,35 +59,30 @@ public class BitmapCache {
// Use 1/5th of the available memory for this memory cache.
final int cacheSize = 1024 * 1024 * memClass / 5;
- Log.i(TAG, "LRUCache size set to " + cacheSize);
+ Log.i(TAG, "LRUCache size set to " + Strings.readableSize(cacheSize));
- mMemCache = new LruCache<String, CacheableBitmap>(cacheSize) {
+ mMemCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
- protected int sizeOf(String key, CacheableBitmap value) {
- return value.getSize();
+ protected int sizeOf(String key, Bitmap value) {
+ return value.getRowBytes() * value.getHeight();
}
};
}
public synchronized Bitmap getBitmapFromMemCache(String key) {
- final CacheableBitmap cacheableBitmap = mMemCache.get(key);
- if (cacheableBitmap == null)
- return null;
- Bitmap b = cacheableBitmap.get();
+ final Bitmap b = mMemCache.get(key);
+
if (b == null){
mMemCache.remove(key);
return null;
}
- if (LOG_ENABLED)
- Log.d(TAG, (b == null) ? "Cache miss" : "Cache found");
return b;
}
public synchronized void addBitmapToMemCache(String key, Bitmap bitmap) {
if (key != null && bitmap != null && getBitmapFromMemCache(key) == null) {
- final CacheableBitmap cacheableBitmap = new CacheableBitmap(bitmap);
- mMemCache.put(key, cacheableBitmap);
+ mMemCache.put(key, bitmap);
}
}
@@ -117,28 +107,4 @@ public class BitmapCache {
}
return bitmap;
}
-
- private static class CacheableBitmap {
- final int mSize;
- final SoftReference<Bitmap> mReference;
-
- CacheableBitmap(Bitmap bitmap){
- mReference = new SoftReference<Bitmap>(bitmap);
- mSize = bitmap == null ? 0 : bitmap.getRowBytes() * bitmap.getHeight();
- }
-
- public SoftReference<Bitmap> getReference(){
- return mReference;
- }
-
- public Bitmap get(){
- if (mReference != null)
- return mReference.get();
- return null;
- }
-
- public int getSize(){
- return mSize;
- }
- }
}
More information about the Android
mailing list