0
votes

I was trying to monitor the memory usage of my game using jconsole (The build in tools from the JDK). I found out that the gc is called every about 3 minutes because the heap allocation increased by about 3 MB every 3 seconds. I'm trying to look for the cause by simplifying my render loop to be as simple as possible, until the only code left is this :

public void render() {

    camera.update();
    Gdx.gl.glClearColor(0.5f, 0.2f, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.setProjectionMatrix(camera.combined);
    batch.setColor(1f, 1f, 1f, 1);

    for( int y = 0 ; y < l1BlockedNd.size ; y++){
        batch.draw(pmxTexture[0], l1BlockedNd.get(y).getPosX() * 10, l1BlockedNd.get(y).getPosY() * 10);
    }

}

When I removed the batch.draw call, I found out that the heap is no longer increasing... It's strange since I didn't make any new object at all. What have I done wrong?

2
Well the only places, where allocations are possible are: l1BlockedNd.get(y) or .getPosX() and .getPosY(). What is l1BlockedNd? Can you show the code behind those methods?Arctic45

2 Answers

0
votes

It's turned out that the batch.draw() method seems to have a little memory overhead (very little actually). The problem arise because I tried a draw a large pool of objects in each frame (about 90000 objects). It's solved by simply reduce the amount of objects drawn by selecting only those that's currently visible to the player.

0
votes

I know you said that you solved your problem...however, one additional thing that you need to do that is not in the code you provided is call batch.end() after all of your batch.draw(...) calls.

public void render() {
    camera.update();
    Gdx.gl.glClearColor(0.5f, 0.2f, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.setProjectionMatrix(camera.combined);
    batch.setColor(1f, 1f, 1f, 1);

    for( int y = 0 ; y < l1BlockedNd.size ; y++){
        batch.draw(pmxTexture[0], l1BlockedNd.get(y).getPosX() * 10, l1BlockedNd.get(y).getPosY() * 10);
    }

    batch.end();
}

batch.end() is what actually draws the stuff in the batch. If you never call it yourself then the only time the batch draws is when it completely fills up and flushes.