Reputation: 1171
We're making a 2D topdown shooter game for our school assignment. But we ran into a problem which we just can't solve. We receive players and want to render them all. When we comment the code which is given, the lag is gone and the RAM usage is stable. However, when we leave the part in, the ram will increase every second with 50 MB/s.
We can't find an error in this code, if you know something please let us know. If you need more information just let me know, I will try to answer them ASAP.
List<SimplePlayer> localMultiplayers = new ArrayList(this.multiplayers);
this.multiplayers.clear();
for (SimplePlayer splayer : localMultiplayers) {
try {
Player p = new Player(splayer, this);
batch.setProjectionMatrix(game.camera.combined);
batch.begin();
p.render(batch);
this.fontwhite.draw(batch, splayer.getName(), splayer.getX(), splayer.getY() + 76);
this.fontred.draw(batch, Integer.toString(splayer.getHitPoints()), splayer.getX() + 12, splayer.getY() + 64);
batch.end();
} catch (Exception ex) {
Logger.getLogger(GameScreen.class.getName()).log(Level.SEVERE, null, ex);
}
}
EDIT: This piece of code is found in our GameScreen.render(), we don't know if that could be the issue.
Upvotes: 0
Views: 1352
Reputation: 459
Guido, no kidding it leaks :-). Every render cycle (approx 60 times per second), you're creating new Player instances for each of your clients which (I can only assume) have a Texture of some sort in them. You must call Texture.dispose() if you no longer want to use it - otherwise it leaks.
The fix is to create the Player List separately (add/remove Players as they join/leave the game) and keep calling their render methods without creating new instances.
Upvotes: 3