我的游戏有内存泄漏

时间:2015-05-11 16:33:25

标签: android memory-leaks

我正在开发一款简单的游戏。我正在监视堆大小,它正在慢慢增加到最大限制为48mb。但是,我在代码中看不到会导致如此高内存使用量的任何内容。我正在做的就是在屏幕上绘制九个圆圈并在它们周围设置阴影。任何帮助将不胜感激!

 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);










}

2 个答案:

答案 0 :(得分:1)

可能所有对drawCircle的调用都在堆中创建局部变量。垃圾收集器应该照顾它们,这就是它被封锁的原因。你有什么问题吗?或者只是一个问题?

答案 1 :(得分:0)

http://www.curious-creature.com/2008/12/18/avoid-memory-leaks-on-android/comment-page-1/有一些关于Android中内存泄漏的内容,方法和方法的有趣背景。