为什么每次切换屏幕时我的libgdx游戏都会使用更多RAM?

时间:2016-04-04 19:04:37

标签: java memory-leaks libgdx

到目前为止,我制作了一个包含两个屏幕的游戏,一个主菜单和一个游戏屏幕。但是我注意到,每次我从主菜单切换到游戏屏幕时,它会耗尽50-60mb以上的RAM。不是太多,但并非全部。

当我返回主菜单时,它下降到30-40mb。当我再次上场比赛时,它再次上升到50-60mb。这样,我在屏幕上切换的次数就会越来越多。它也会在游戏画面上慢慢开始使用更多的RAM。不像屏幕切换那么大,但仍然如此。

我试图尽可能多地处理并且我也试图使用gc,但它仍然不会停止这种行为。这里是我的一些代码:

游戏课

 public class MyGame extends Game {

public OrthographicCamera camera;
public SpriteBatch batch;

public static final int WIDTH = 800;
public static final int HEIGHT = 450;

public static MainMenu mainmenu;

@Override
public void create() {

    mainmenu = new MainMenu(this);

    camera = new OrthographicCamera();
    camera.setToOrtho(true, 1920, 1080);

    batch = new SpriteBatch();

    setScreen(mainmenu);
}

public void dispose(){
    this.getScreen().dispose();
    batch.dispose();
}

}

主菜单类:

public class MainMenu implements Screen{

@SuppressWarnings("unused")
private MyGame game;

private Stage stage;
private Sprite bground;
private SpriteDrawable bgsprite;
private TextureAtlas atlas;
private Button playbutton, aboutbutton;
private Table table;
private Skin skin;
private Sound btnclick;

public MainMenu(final MyGame game){
    this.game = game;

    stage = new Stage();
    Gdx.input.setInputProcessor(stage);

    btnclick = Gdx.audio.newSound(Gdx.files.internal("sounds/button_click.wav"));

    atlas = new TextureAtlas("atlas/newbuttons.pack");
    skin = new Skin(atlas);
    table = new Table(skin);
    table.setBounds(0, 0, Gdx.graphics.getWidth(),        Gdx.graphics.getHeight());

    Texture bground_texture = new Texture("grass_HD_new.png");
    bground_texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    bground = new Sprite(bground_texture);

    bgsprite = new SpriteDrawable(bground);

    ButtonStyle playbuttonstyle = new ButtonStyle();
    playbuttonstyle.up = skin.getDrawable("play_button_up");
    playbuttonstyle.down = skin.getDrawable("play_button_down");
    playbutton = new Button(playbuttonstyle);
    playbutton.addListener(new ChangeListener() {
         @Override
         public void changed(ChangeEvent event, Actor actor) {
             btnclick.play(0.1F);
             ((Game) Gdx.app.getApplicationListener()).setScreen(new GameScreen(game));
             dispose(); 
         }
    });

    ButtonStyle aboutbuttonstyle = new ButtonStyle();
    aboutbuttonstyle.up = skin.getDrawable("about_button_up");
    aboutbuttonstyle.down = skin.getDrawable("about_button_down");
    aboutbutton = new Button(aboutbuttonstyle);
    aboutbutton.addListener(new ChangeListener() {
         @Override
         public void changed(ChangeEvent event, Actor actor) {
             btnclick.play(0.3F);
         }
    });

    table.add(playbutton).spaceBottom(30);
    table.row();
    table.add(aboutbutton);
    table.background(bgsprite);
    stage.addActor(table);

}

@Override
public void show() {

}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(1f, 1f, 1f, 1f);
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

    stage.act(delta);
    stage.draw();
}

public void update(float delta){

}

@Override
public void resize(int width, int height) {

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {
    stage.dispose();
    bgsprite.getSprite().getTexture().dispose();
    bground.getTexture().dispose();
    atlas.dispose();
    skin.dispose();
    btnclick.dispose();
}
}

游戏画面中的渲染方法: (这个课程很大,所以我不想包括所有课程)

    @Override
public void render(float delta) {
    Gdx.gl.glClearColor(1f, 1f, 1f, 1f);
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

    game.camera.update();

    if(paused || bombTouched){
        if(Gdx.input.isKeyJustPressed(Keys.ESCAPE)){
            paused = false;
        }
        if(Gdx.input.isKeyJustPressed(Keys.M)){
            ((Game) Gdx.app.getApplicationListener()).setScreen(new MainMenu(game));
            dispose();
        }
        if(Gdx.input.isKeyJustPressed(Keys.R)){
            bombTouched = false;
        }
        if(Gdx.input.isKeyJustPressed(Keys.Q)){
            Gdx.app.exit();
        }
    }else{
        update(delta);
    }

    game.batch.setProjectionMatrix(game.camera.combined);

    game.batch.begin();

        game.batch.draw(sprite, 0, 0);


        for(Coin coin : coin.rect) {
            game.batch.draw(coin.getSprite(), coin.getBounds().x, coin.getBounds().y);
        }

        for(Bomb bomb : bomb.rect) {
            game.batch.draw(bomb.getSprite(), bomb.getBounds().x, bomb.getBounds().y);
        }


        game.batch.draw(snail.getSprite(), snail.getBounds().x, snail.getBounds().y);

        font.draw(game.batch, "score:   " + score, 100, 980);
        fps.draw(game.batch, "fps: " + Gdx.graphics.getFramesPerSecond(), 20, 30);

        if(paused){
            game.batch.draw(pause_sprite, 0, 0);
        }

        if(bombTouched){
            game.batch.draw(gameover_sprite, 0, 0);
        }

    game.batch.end();
}

0 个答案:

没有答案