1
\$\begingroup\$

So basically in my game(completely new to gave dev) I have a level select screen. I have 8 Buttons to select the levels and i use an asset manager to load the textures for the buttons. For example their is a locked button for when you have unlocked the level, an active for when your mouse is over the button and inactive when your mouse is not over it. My problem is that having all these textures keep interchanging is causing memory usage to go off the charts, from as low as 30mb all the way over 1000mb as soon as i go to the level select screen and it keeps rising. Whats a good way to load lots of textures without having to use so much memory? Or should i just figure out a better way to load them without causing so much usage of memory. Im new to this so go easy on me :)

or... Would this amount of memory being sprung be a memory leak somewhere?

Edit: Heres the code snippet where the memory leak i traced to i believe.

I'm calling this method in my handleInput which i then call in the render method.

public void levelManager(float rowX, float rowY, Screen setScreen, Texture drawActive, Texture drawInactive,
        float multiplier) {
    if (Gdx.input.getX() / GravitySpikesRevamped.PPM < (rowX + BUTTON_WIDTH) / GravitySpikesRevamped.PPM
            && Gdx.input.getX() / GravitySpikesRevamped.PPM > rowX / GravitySpikesRevamped.PPM
            && (GravitySpikesRevamped.GAME_HEIGHT - Gdx.input.getY())
                    / GravitySpikesRevamped.PPM < (rowY + BUTTON_HEIGHT) / GravitySpikesRevamped.PPM
            && (GravitySpikesRevamped.GAME_HEIGHT - Gdx.input.getY()) / GravitySpikesRevamped.PPM > rowY
                    / GravitySpikesRevamped.PPM) {

        if (multiplier == 1) {
            game.batch.draw(drawActive, BUTTON_WIDTH / 2 / GravitySpikesRevamped.PPM,
                    rowY / GravitySpikesRevamped.PPM, BUTTON_WIDTH / GravitySpikesRevamped.PPM,
                    BUTTON_HEIGHT / GravitySpikesRevamped.PPM);
        } else if (multiplier == 2) {
            game.batch.draw(drawActive, BUTTON_WIDTH * 2 / GravitySpikesRevamped.PPM,
                    rowY / GravitySpikesRevamped.PPM, BUTTON_WIDTH / GravitySpikesRevamped.PPM,
                    BUTTON_HEIGHT / GravitySpikesRevamped.PPM);
        } else if (multiplier == 3) {
            game.batch.draw(drawActive, BUTTON_WIDTH * 3.5f / GravitySpikesRevamped.PPM,
                    rowY / GravitySpikesRevamped.PPM, BUTTON_WIDTH / GravitySpikesRevamped.PPM,
                    BUTTON_HEIGHT / GravitySpikesRevamped.PPM);
        }

        if (Gdx.input.isTouched()) {
            game.setScreen(setScreen);
        }
    } else {

        if (multiplier == 1) {
            game.batch.draw(drawInactive, BUTTON_WIDTH / 2 / GravitySpikesRevamped.PPM,
                    rowY / GravitySpikesRevamped.PPM, BUTTON_WIDTH / GravitySpikesRevamped.PPM,
                    BUTTON_HEIGHT / GravitySpikesRevamped.PPM);
        } else if (multiplier == 2) {
            game.batch.draw(drawInactive, BUTTON_WIDTH * 2 / GravitySpikesRevamped.PPM,
                    rowY / GravitySpikesRevamped.PPM, BUTTON_WIDTH / GravitySpikesRevamped.PPM,
                    BUTTON_HEIGHT / GravitySpikesRevamped.PPM);
        } else if (multiplier == 3) {
            game.batch.draw(drawInactive, BUTTON_WIDTH * 3.5f / GravitySpikesRevamped.PPM,
                    rowY / GravitySpikesRevamped.PPM, BUTTON_WIDTH / GravitySpikesRevamped.PPM,
                    BUTTON_HEIGHT / GravitySpikesRevamped.PPM);
        }
    }

Okay i solved it, basically in my method above every time i called it was creating a new screen. So i basically was creating infinite amount screens in memory. I fixed it :]

\$\endgroup\$
  • \$\begingroup\$ "Would this amount of memory being sprung be a memory leak somewhere?" If your screen only uses a finite number of assets, but memory usage keeps increasing endlessly, then you definitely have a memory leak. It's tough to diagnose where exactly without knowing the details of your asset manager and how you're using it to avoid duplicating assets in memory or leaving gaps of memory that you don't re-use after unloading an asset. Can you post a minimal snippet of code that demonstrates the problem? \$\endgroup\$ – DMGregory Jul 29 '16 at 17:10
  • \$\begingroup\$ Yeah, its definitely in the method i posted, tried to make my buttons simpler but ended up causing massive memory leak somewhere in it. I have my old code that i used but is long and doesnt cause the memory leak but this method does. Is it possible that because im technically created a "new screen" for everytime i call the method and just keeps creating new screens over and over again in memory? \$\endgroup\$ – CabDude Jul 29 '16 at 17:34
  • \$\begingroup\$ If you use Scene2D, there's Button class in which the normal image, click image, hover image etc can be set. \$\endgroup\$ – WeirdElfB0y Aug 3 '16 at 10:57
0
\$\begingroup\$

Okay, so I had a memory leak. In my method I posted i was calling it in my render so basically a loop of creating new screens over and over again which was destroying away my memory.

levelManager(firstX, ROW_FOUR_BUTTON_Y, new LevelSevenScreen(game),
                    game.textureManager.get("LevelSevenButtonActive.png", Texture.class),
                    game.textureManager.get("LevelSevenButtonInactive.png", Texture.class), 1);

I was basically doing that everytime i called it which was a no no.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.