Reputation: 73
I have create a game app in libgdx and usually it works well, but sometimes it goes in slow motion, in my opinion is a RAM memory problem. I've a Main class that extends Game.class, in main class i create a play screen class, when the player died i create again the play screen class. I believe that the RAM memory that the memory has not released and after many death it accumulates, in fact if I run the application with the task manager open when the death increase memory also increases.
This is the code:
public class MyGdxGame extends Game(){
private PlayScreen play_screen;
private SpriteBatch batch;
public void create(){
batch=new SpriteBatch();
play_screen=new PlayScreen(this);
setScreen(play_screen);
}
public void render(){
if(play_screen.death==true){
play_screen=new PlayScreen(this);
setScreen(play_screen);
}
}
So I did a test:
public void render(){
do{
play_screen=null;
play_screen=new PlayScreen(this);
setScreen(play_screen);
}while(1!=2);
}
I ran the app with the task manager open, memory increases rapidly until it crashes. So how i can clean up the RAM memory?
Upvotes: 0
Views: 398
Reputation:
Many LibGDX objects has to be manually cleared for memory, see this. They implement the interface Disposable which has the method #dispose()
.
I don't see you disposing any of your used resources, your SpriteBatch is one of these objects.
When you're done with it call batch.dispose()
. Setting it to null afterwards is optional, but recommended because using a disposed resource might lead to unintended behavior. The code will look something like this, which should be called when you no longer need it:
if(batch != null) {
batch.dispose();
batch = null;
}
LibGDX memory leaks is almost ever because one or more resources was not disposed
Upvotes: 1