我正在設計一個應用程序,我希望將12位圖存儲在內存中以便快速訪問。我正在從磁盤緩存中檢索這些圖像,或者如果它們不存在,請將其下載。我遇到的問題是幾次運行緩存後我的應用程序崩潰的活動,但異常內存不足異常緩存位圖時出現異常
只有當Runtime.getRuntime().totalMemory()超過66265056
05-10 15:32:44.831: E/dalvikvm-heap(26563): Out of memory on a 6554896-byte allocation. 05-10 15:32:44.831: I/dalvikvm(26563): "main" prio=5 tid=1 RUNNABLE 05-10 15:32:44.831: I/dalvikvm(26563): | group="main" sCount=0 dsCount=0 obj=0x411d96a0 self=0x411c2988 05-10 15:32:44.831: I/dalvikvm(26563): | sysTid=26563 nice=0 sched=0/0 cgrp=apps handle=1075505232 05-10 15:32:44.831: I/dalvikvm(26563): | schedstat=(0 0 0) utm=872 stm=205 core=0 05-10 15:32:44.831: I/dalvikvm(26563): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 05-10 15:32:44.831: I/dalvikvm(26563): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:592) 05-10 15:32:44.831: I/dalvikvm(26563): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445) 05-10 15:32:44.831: I/dalvikvm(26563): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781) 05-10 15:32:44.831: I/dalvikvm(26563): at android.content.res.Resources.loadDrawable(Resources.java:1963) 05-10 15:32:44.831: I/dalvikvm(26563): at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 05-10 15:32:44.831: I/dalvikvm(26563): at android.view.View.<init>(View.java:3424) 05-10 15:32:44.831: I/dalvikvm(26563): at android.view.View.<init>(View.java:3357) 05-10 15:32:44.831: I/dalvikvm(26563): at android.view.ViewGroup.<init>(ViewGroup.java:426) 05-10 15:32:44.831: I/dalvikvm(26563): at android.widget.RelativeLayout.<init>(RelativeLayout.java:184) 05-10 15:32:44.831: I/dalvikvm(26563): at java.lang.reflect.Constructor.constructNative(Native Method) 05-10 15:32:44.831: I/dalvikvm(26563): at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 05-10 15:32:44.831: I/dalvikvm(26563): at android.view.LayoutInflater.createView(LayoutInflater.java:587) 05-10 15:32:44.831: I/dalvikvm(26563): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 05-10 15:32:44.831: I/dalvikvm(26563): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660) 05-10 15:32:44.831: I/dalvikvm(26563): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) 05-10 15:32:44.831: I/dalvikvm(26563): at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 05-10 15:32:44.831: I/dalvikvm(26563): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 05-10 15:32:44.831: I/dalvikvm(26563): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 05-10 15:32:44.831: I/dalvikvm(26563): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309) 05-10 15:32:44.831: I/dalvikvm(26563): at android.app.Activity.setContentView(Activity.java:1914) 05-10 15:32:44.831: I/dalvikvm(26563): at com.afewguys.photofrenzy.activities.PreGameActivity_.setContentView(PreGameActivity_.java:48) 05-10 15:32:44.831: I/dalvikvm(26563): at com.afewguys.photofrenzy.activities.PreGameActivity_.onCreate(PreGameActivity_.java:31) 05-10 15:32:44.831: I/dalvikvm(26563): at android.app.Activity.performCreate(Activity.java:5179) 05-10 15:32:44.831: I/dalvikvm(26563): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 05-10 15:32:44.831: I/dalvikvm(26563): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074) 05-10 15:32:44.831: I/dalvikvm(26563): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 05-10 15:32:44.831: I/dalvikvm(26563): at android.app.ActivityThread.access$700(ActivityThread.java:143) 05-10 15:32:44.831: I/dalvikvm(26563): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241) 05-10 15:32:44.831: I/dalvikvm(26563): at android.os.Handler.dispatchMessage(Handler.java:99) 05-10 15:32:44.831: I/dalvikvm(26563): at android.os.Looper.loop(Looper.java:137) 05-10 15:32:44.831: I/dalvikvm(26563): at android.app.ActivityThread.main(ActivityThread.java:4950) 05-10 15:32:44.831: I/dalvikvm(26563): at java.lang.reflect.Method.invokeNative(Native Method) 05-10 15:32:44.831: I/dalvikvm(26563): at java.lang.reflect.Method.invoke(Method.java:511) 05-10 15:32:44.831: I/dalvikvm(26563): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 05-10 15:32:44.831: I/dalvikvm(26563): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 05-10 15:32:44.831: I/dalvikvm(26563): at dalvik.system.NativeStart.main(Native Method) 05-10 15:32:44.831: A/libc(26563): Fatal signal 11 (SIGSEGV) at 0x0031fb00 (code=1), thread 26584 (uys.photofrenzy) 05-10 15:32:44.831: A/libc(26563): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 26563 (uys.photofrenzy)
我存儲從Android的文檔建議在LRUCache位圖。 DiskLRUCache用於磁盤緩存。我無法使用DiskLRUCache的內存緩存部分,因爲它無法隨機檢索圖像。我做緩存的擴展應用類
final int maxMemory = (int) (Runtime.getRuntime().maxMemory()/1024);
final int cacheSize = maxMemory/8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@SuppressLint("NewApi")
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getByteCount()/1024;
}
@Override
protected void entryRemoved(boolean evicted, String key, Bitmap oldBitmap, Bitmap newBitmap) {
oldBitmap.recycle();
oldBitmap = null;
}
};
}
public static void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public static void clearCache(){
mMemoryCache.evictAll();
System.gc();
}
public static Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
的活動爲cacheing
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
prefs = this.getSharedPreferences("com.afewguys.photofrenzy", Context.MODE_PRIVATE);
File diskCacheLocation = getExternalFilesDir(C.thumbNailDir);
BitmapLruCache.Builder builder = new BitmapLruCache.Builder(this);
builder.setMemoryCacheEnabled(false).setMemoryCacheMaxSizeUsingHeapSize();
builder.setDiskCacheEnabled(true).setDiskCacheLocation(diskCacheLocation);
mCache = builder.build();
game = DatabaseManager.getInstance().getGameEntryWithId(getIntent().getExtras().getString(C.bundleGame));
mCache = builder.build();
PhotoFrenzyApplication.clearCache();
new loadSingleView().execute();
}
class loadSingleView extends AsyncTask<String, String, String> {
int round = 0;
int numPhoto = 0;
@Override
protected String doInBackground(String... args) {
PhotoFrenzyApplication.clearHash();
for(PhotoRound pr:game.getDbPhotoRounds()){
round++;
numPhoto = 0;
for(Photo p:pr.getDBPhotos()){
CacheableBitmapDrawable cView = mCache.get(p.getThumbNailLink());
Bitmap bm = (cView != null) ? mCache.get(p.getThumbNailLink()).getBitmap() : null;
if(bm != null){
PhotoFrenzyApplication.addBitmapToMemoryCache(p.getThumbNailLink(),mCache.get(p.getThumbNailLink()).getBitmap());
mCache.remove(p.getThumbNailLink());
}
else
PhotoFrenzyApplication.addBitmapToMemoryCache(p.getThumbNailLink(),loadNewPhoto(p.getThumbNailLink())); PhotoFrenzyApplication.addImageToHash(loadNewPhoto(p.getOriginalLink()),p.getOriginalLink());
System.out.println("Memory : " + Runtime.getRuntime().totalMemory());
publishProgress();
}
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
updateRoundPhotosLoaded(round,++numPhoto);
}
@Override
protected void onPostExecute(String args) {
startGame();
}
}
我已驗證驅逐舊位圖時循環物獲取調用。我找不到內存在哪裏