2011-07-13 77 views
0

所以我遇到了一些錯誤,我搜查了起來。看來我有一些內存問題。但我不太清楚如何解決它。這裏的代碼,我相信問題在於,還有錯誤日誌。任何幫助,將不勝感激下面Android錯誤:內存問題?

static List<Dragon> mdragon = new ArrayList<Dragon>(); 
private int mdragon_number = 0; 

    @Override 
public boolean onTouchEvent(MotionEvent event) { 
    synchronized (mdragon) { 
     mdragon.add(new Dragon(getResources(), (int) event.getX(), 
       (int) event.getY())); 
     mdragon_number = mdragon.size(); 
    } 
    return super.onTouchEvent(event); 
} 

public void Remove(long elapsed, Canvas canvas) { 
    synchronized (mdragon) { 
     List<Dragon> toRemove = new ArrayList<Dragon>(); 
     for (Dragon dragon : mdragon) { 
      if (Condition to remove an element) 
       toRemove.add(dragon); 
      } 
      mdragon.removeAll(toRemove); 
    } 
} 

錯誤日誌:

07-13 02:11:46.814: ERROR/AndroidRuntime(280): FATAL EXCEPTION: main 
07-13 02:11:46.814: ERROR/AndroidRuntime(280): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 
07-13 02:11:46.814: ERROR/AndroidRuntime(280):  at android.graphics.Bitmap.nativeCreate(Native Method) 
07-13 02:11:46.814: ERROR/AndroidRuntime(280):  at android.graphics.Bitmap.createBitmap(Bitmap.java:468) 
07-13 02:11:46.814: ERROR/AndroidRuntime(280):  at android.graphics.Bitmap.createBitmap(Bitmap.java:435) 
07-13 02:11:46.814: ERROR/AndroidRuntime(280):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340) 
07-13 02:11:46.814: ERROR/AndroidRuntime(280):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488) 
07-13 02:11:46.814: ERROR/AndroidRuntime(280):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462) 
07-13 02:11:46.814: ERROR/AndroidRuntime(280):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323) 
07-13 02:11:46.814: ERROR/AndroidRuntime(280):  at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:346) 
07-13 02:11:46.814: ERROR/AndroidRuntime(280):  at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:372) 

回答

0

你可能存儲在虛擬內存的位圖圖像未得到及時解除分配引用/當你活動完成。因此no more memory留待玩。

嘗試實施一種不將圖像存儲在內存中的邏輯,如果圖像很大,也可以嘗試縮小圖像。

0

恕我直言,你的問題與BitmapFactory選項有關。爲了節省VM資源,你最好使用專門的選項BitmapFactory

BitmapFactory.Options options=new BitmapFactory.Options(); 
options.inPurgeable=true; //declare as purgeable to disk 
Bitmap bitmap=BitmapFactory.decodeResource(context, R.drawable.myDrawable, options);