Android Error: Memory Issues?
so i came across some errors and i've searched it up. it seems i have some memory issues. but im not quite sure on how to solve it. here's the codes where i believe the problem lies with and also the error log. any help would be highly appreciated
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);
}
}
Error Log below:
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/Androi开发者_开发技巧dRuntime(280): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:372)
You are probably storing references to some bitmap images in the vm memory which are not getting de-allocated in time / when you activity gets finished . Hence no more memory is left to play with.
Try implementing a logic that does not store images in memory, also try scaling down the images if they are huge.
IMHO your problem is related to BitmapFactory
options. In order to save VM resources you'd better use special options for BitmapFactory
:
BitmapFactory.Options options=new BitmapFactory.Options();
options.inPurgeable=true; //declare as purgeable to disk
Bitmap bitmap=BitmapFactory.decodeResource(context, R.drawable.myDrawable, options);
You are running out of bitmap memory in the native heap - see BitmapFactory OOM driving me nuts
精彩评论