2013-05-10 126 views
0

我正在设计一个应用程序,我希望将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(); 
    } 
} 

我已验证驱逐旧位图时循环物获取调用。我找不到内存在哪里

回答

0

首先我建议不要在内存中保留这么大的位图。 6554896字节= 1280x1280图像,它不适合大多数显示器。

您似乎有缓存在两个地方:ApplicationActivity,这是奇怪的。

我建议使用MAT(Eclipse内存分析器)来搜索内存泄漏Activity。如果你发现超过1个,你的根源就是崩溃。

除此之外,你的代码中有一些邪恶的东西,如oldBitmap.recycle();System.gc();