[英]java.lang.OutOfMemoryError: bitmap size exceeds VM budget crashes on second time running app
hello i know this is a dupicat tread but the answer did not seem to work for me my app crashes on second time running so if i run it once its ok but when i close and run again it crashes giving me this error in the log cat java.lang.OutOfMemoryError: bitmap size exceeds VM budget i tried using this code 你好,我知道这是一个双重的问题,但是答案似乎并不适合我,我的应用程序在第二次运行时崩溃,所以如果我一次运行就可以了,但是当我关闭并再次运行时,它崩溃了,这让我在日志猫中看到了这个错误java.lang.OutOfMemoryError:位图大小超出了我尝试使用此代码的VM预算
@Override
protected void onDestroy(){
// TODO Auto-generated method stub
super.onDestroy();
unbindDrawables(sView);
System.gc();
}
private void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
but no luck i am drawing my bitmaps on a canvas using the surfaceview class this is my code private SurfaceV sView; 但是运气不好,我正在使用surfaceview类在画布上绘制位图,这是我的代码私有SurfaceV sView;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
sView = new SurfaceV(this);
setContentView(sView);
}
and in the sView class 并在sView类中
private SurfaceHolder holder;
private Thread thread = null;
private Canvas canvas;
private Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.sky);
private Bitmap bg = Bitmap.createScaledBitmap(background, w, h, false);
private Bitmap sky = BitmapFactory.decodeResource(getResources(), R.drawable.sky2);
private Bitmap s = Bitmap.createBitmap(sky, 210, 10, 230, 230);
private Bitmap c1 = Bitmap.createBitmap(sky, 10, 14, 182, 100);
private Bitmap c2 = Bitmap.createBitmap(sky, 10, 142, 182, 100);
private Bitmap c3 = Bitmap.createBitmap(sky, 10, 292, 182, 112);
private Bitmap sun = Bitmap.createScaledBitmap(s, sunSize, sunSize, false);
private Bitmap cloud1 = Bitmap.createScaledBitmap(c1, cloudWidth, cloudHeigth, false);
private Bitmap cloud2 = Bitmap.createScaledBitmap(c2, cloudWidth, cloudHeigth, false);
private Bitmap cloud3 = Bitmap.createScaledBitmap(c3, cloudWidth, cloudHeigth, false);
public SurfaceV(Context context){
super(context);
holder = getHolder();
}
@Override
public void run(){
while(running){
if(!holder.getSurface().isValid()){
continue;
}
startTime = System.currentTimeMillis();
canvas = holder.lockCanvas();
draw(canvas);
holder.unlockCanvasAndPost(canvas);
doFpsCheck(startTime);
}
@Override
public void draw(Canvas canvas){
super.draw(canvas);
canvas.drawBitmap(bg, 0, 0, null);
canvas.drawBitmap(sun, 20, 20, null);
canvas.drawBitmap(cloud1, c1x, c1y, null);
canvas.drawBitmap(cloud2, c2x, c2y, null);
canvas.drawBitmap(cloud3, c3x, c3y, null);
}
please any help will be very much appriciated thank you in advance 请任何帮助将非常感谢您提前
The memory leak in Android when you use Bitmaps is very popular. 使用位图时,Android中的内存泄漏非常流行。 It's because you create many bitmaps, display them, but not removing them from memory, so suddenly you will get out of memory error.
这是因为您创建了许多位图,将其显示,但未将其从内存中删除,因此突然您将遇到内存不足错误。
After creating bitmaps and drawing them you should use Recycle method: 创建位图并绘制它们之后,应使用Recycle方法:
yourBitmap.recycle() yourBitmap.recycle()
This is form official documentation: 这是官方文件:
"Caution: You should use recycle() only when you are sure that the bitmap is no longer being used. If you call recycle() and later attempt to draw the bitmap, you will get the error: "Canvas: trying to use a recycled bitmap" “警告:只有在确定不再使用位图时,才应使用recycle()。如果调用recycle()并在以后尝试绘制位图,则会收到错误消息:“画布:尝试使用位图回收位图”
More you can find here: 您可以在这里找到更多信息:
https://developer.android.com/training/displaying-bitmaps/manage-memory.html https://developer.android.com/training/displaying-bitmaps/manage-memory.html
Hope it will help. 希望它会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.