Tool:
private static LruMemoryCache<String, SoftReference<Bitmap>> mMemoryCache;
private static final int sMinMemoryCacheSize = 1024 * 1024 * 2 ; // 2MB
private static int sMemoryCacheSize = 1024 * 1024 * 4; // 4MB
/**
* @param memoryCacheSize
*/
public static void setMemoryCacheSize(int memoryCacheSize){
if( memoryCacheSize>=memoryCacheSize)
sMemoryCacheSize= memoryCacheSize;
}
public static synchronized LruMemoryCache<String, SoftReference<Bitmap>> getLruMemoryCache(){
if (mMemoryCache != null) {
try {
clearMemoryCache();
} catch (Exception e) {
e.printStackTrace();
}
}
mMemoryCache = new LruMemoryCache<String, SoftReference<Bitmap>>(
sMemoryCacheSize) {
@Override
protected int sizeOf(String key, SoftReference<Bitmap> bitmapRef) {
return BitmapCommonUtils.getBitmapSize(bitmapRef.get());
}
};
return mMemoryCache;
}
/**
*clearMemoryCache
*/
public synchronized static void clearMemoryCache() {
if (mMemoryCache != null) {
mMemoryCache.evictAll();
// Add
mMemoryCache=null;
}
}
public synchronized static void addBitmapToMemoryCache(final String key, final Bitmap bitmap) {
if (getBitmapFromMemoryCache(key) == null) {
mMemoryCache.put(key, new SoftReference<Bitmap>(bitmap));
}
}
public synchronized static Bitmap getBitmapFromMemoryCache(final String key) {
// Add
if ( null == mMemoryCache)
getLruMemoryCache();
// Add
if (mMemoryCache != null) {
SoftReference<Bitmap> softRef = mMemoryCache.get(key);
return softRef == null ? null : softRef.get();
}
return null;
}
ERR:
12-10 06:20:41.862: E/art(1680): Throwing OutOfMemoryError "Failed to allocate a 9938680 byte allocation with 5628604 free bytes and 5MB until OOM"
12-10 06:20:41.863: E/AndroidRuntime(1680): Error reporting crash
12-10 06:20:41.863: E/AndroidRuntime(1680): java.lang.OutOfMemoryError: Failed to allocate a 9938680 byte allocation with 5628604 free bytes and 5MB until OOM
12-10 06:20:41.863: E/AndroidRuntime(1680): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
12-10 06:20:41.863: E/AndroidRuntime(1680): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:125)
12-10 06:20:41.863: E/AndroidRuntime(1680): at java.lang.StringBuffer.append(StringBuffer.java:278)
12-10 06:20:41.863: E/AndroidRuntime(1680): at java.io.StringWriter.write(StringWriter.java:123)
12-10 06:20:41.863: E/AndroidRuntime(1680): at com.android.internal.util.FastPrintWriter.flushLocked(FastPrintWriter.java:358)
12-10 06:20:41.863: E/AndroidRuntime(1680): at com.android.internal.util.FastPrintWriter.appendLocked(FastPrintWriter.java:303)
12-10 06:20:41.863: E/AndroidRuntime(1680): at com.android.internal.util.FastPrintWriter.write(FastPrintWriter.java:625)
12-10 06:20:41.863: E/AndroidRuntime(1680): at com.android.internal.util.FastPrintWriter.append(FastPrintWriter.java:658)
12-10 06:20:41.863: E/AndroidRuntime(1680): at java.io.PrintWriter.append(PrintWriter.java:691)
12-10 06:20:41.863: E/AndroidRuntime(1680): at java.io.PrintWriter.append(PrintWriter.java:31)
12-10 06:20:41.863: E/AndroidRuntime(1680): at java.lang.Throwable.printStackTrace(Throwable.java:324)
12-10 06:20:41.863: E/AndroidRuntime(1680): at java.lang.Throwable.printStackTrace(Throwable.java:300)
12-10 06:20:41.863: E/AndroidRuntime(1680): at android.util.Log.getStackTraceString(Log.java:335)
12-10 06:20:41.863: E/AndroidRuntime(1680): at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:59)
12-10 06:20:41.863: E/AndroidRuntime(1680): at com.android.internal.os.RuntimeInit.access$200(RuntimeInit.java:43)
12-10 06:20:41.863: E/AndroidRuntime(1680): at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:85)
12-10 06:20:41.863: E/AndroidRuntime(1680): at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
12-10 06:20:41.863: E/AndroidRuntime(1680): at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
this is my bitmap cache pool. it is fine on other version, but android 5.0. it is about ART question?
why bitmap cache will carsh on 5.0?
0 comments:
Post a Comment