Android 我的游戏内存泄漏
我正在做一个简单的游戏。我正在监视堆大小,它正在缓慢地增加到48mb的最大限制。但是,我在代码中没有看到任何会导致如此高内存使用率的内容。我所做的就是在屏幕上画九个圆圈,并在它们周围设置一个阴影。任何帮助都将不胜感激,谢谢Android 我的游戏内存泄漏,android,memory-leaks,Android,Memory Leaks,我正在做一个简单的游戏。我正在监视堆大小,它正在缓慢地增加到48mb的最大限制。但是,我在代码中没有看到任何会导致如此高内存使用率的内容。我所做的就是在屏幕上画九个圆圈,并在它们周围设置一个阴影。任何帮助都将不胜感激,谢谢 public void run() { Log.d("Speed Buttons", "Background thread has started"); //standard game loop Canvas canvas; while(is
public void run() {
Log.d("Speed Buttons", "Background thread has started");
//standard game loop
Canvas canvas;
while(isRunning == true)
{
if(!holder.getSurface().isValid()) {
continue;
}
canvas = holder.lockCanvas();
//call dodraw method
doDraw(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
protected void doDraw(Canvas canvas)
{
Paint circle = new Paint();
circle.setColor(Color.BLUE);
//creates a glow around the circles
circle.setShadowLayer(5,0,0,Color.RED);
op.setWidthSetHeight(getWidth(), getHeight());
int big = 130;
op.setBig(130);
//I have to use getwidth and getheight in draw, because in context, it would include the title bar and status bar
//circle 5
//using convertDP to pixel for everything
canvas.drawCircle(getWidth()/2,getHeight()/2,op.convertDpToPixel(6000),circle);
//circle 2
canvas.drawCircle(getWidth()/2,getHeight()/2-big*scale,op.convertDpToPixel(6000),circle);
//circle 1
canvas.drawCircle(getWidth()/2-(big*scale),getHeight()/2-big*scale,op.convertDpToPixel(6000),circle);
//circle 3
canvas.drawCircle(getWidth()/2+ (big*scale),getHeight()/2-big*scale,op.convertDpToPixel(6000),circle);
//circle 8
canvas.drawCircle(getWidth()/2,getHeight()/2+big*scale,op.convertDpToPixel(6000),circle);
//circle 4
canvas.drawCircle(getWidth()/2-(big*scale),getHeight()/2,op.convertDpToPixel(6000),circle);
//circle 6
canvas.drawCircle(getWidth()/2+(big*scale),getHeight()/2,op.convertDpToPixel(6000),circle);
//circle 7
canvas.drawCircle(getWidth()/2-(big*scale),getHeight() / 2 + big * scale, op.convertDpToPixel(6000), circle);
//circle 9
canvas.drawCircle(getWidth()/2+(big*scale),getHeight()/2+big*scale,op.convertDpToPixel(6000),circle);
}
所有对drawCircle的调用可能都是在堆中创建局部变量。垃圾收集器应该照顾好它们,这就是它封顶的原因。你有什么问题吗?或者这只是一个问题?有一些有趣的背景知识,关于Android中如何避免内存泄漏以及如何避免内存泄漏。如果这是一个问题,你能提供一个解决方案吗?就目前情况而言,这将是一个更有用的注释。对于初学者来说,将
circle
的实例化移出绘图循环似乎会有所帮助。不,这不仅仅是一个问题。我看到可用堆空间在10分钟内从30MB下降到2MB。我不确定如果我让它降到零会发生什么。我在那之前就停止了。我猜是内存不足异常..您可以试试看垃圾收集器是否运行