我正在设计一个应用程序,我希望在内存中存储12位图以便快速访问。我正在从磁盘缓存中检索这些图像,或者如果它们不存在则下载它们。我遇到的问题是运行了几次运行缓存我的应用程序在OnCreate方法中崩溃的活动,但异常
仅当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 Docs推荐的LRUCache中。而DiskLRUCache用于磁盘缓存。我无法使用DiskLRUCache的内存缓存部分,因为它无法在随机时间检索图像。我在扩展的Application类
中进行缓存 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);
}
缓存活动
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 :(得分:0)
首先,我建议不要在内存中保留这么大的Bitmaps。 6554896字节= 1280x1280图像,不适合大多数显示器。
您似乎在两个地方进行了缓存:Application
和Activity
,这很奇怪。
我建议使用MAT(Eclipse Memory Analyzer)来搜索Activity
的内存泄漏。如果您发现超过1,则说明崩溃的根本原因。
除此之外,您的代码中存在一些恶意内容,例如oldBitmap.recycle();
或System.gc();
。