question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android Libgdx memory leak on creating multiple Textures

See original GitHub issue

I’ve created an Android application to show about a hundred point objects using Libgdx modelBuilder. It works fine and models are rendered without any problem, but I needed to add a marker for every point model. To do that I’ve extended from Actor and in this class, I’ve created two textures representing the marker in normal and the selected mode when the user clicked the objects. The following is the LabelActor class which has been instantiated for each point model.

public class LabelActor extends Actor {

    Texture texture;
    Texture selectedTexture;
    Sprite sprite;
    private Vector3 distVector = new Vector3();

    public LabelActor(Texture[] textures){

        texture = textures[0];
        selectedTexture = textures[1];
        sprite = new Sprite(texture);
        setBounds(sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
    }

    @Override
    protected void positionChanged() {
        sprite.setPosition(getX(),getY());
        super.positionChanged();
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {

         if (selected){
            batch.draw(selectedTexture, getX()-sprite.getWidth()/2, getY());
            infoWindow.draw(batch, parentAlpha);
        } else {
            batch.draw(texture, getX()-sprite.getWidth()/2, getY());
        }
    }
}

I’ve used AssetManager to load textures and create HashMap of them in the main screen then pass an array of two textures to each LabelActor class.

@Override
public void create() {

    ...
    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.0f));
    pointLight = new PointLight().set(0.8f, 0.8f, 0.8f, 2f, 0f, 0f, 500f);
    environment.add(pointLight);

    assetManager =  new AssetManager();
    textureTitleList = new ArrayList<>();
    for (FileHandle f: Gdx.files.internal("markers").list()){
        assetManager.load("markers/" + f.name(), Texture.class);
        textureTitleList.add(f.name());
    }
}

private void loadModel() {

    textureMap = new HashMap<>();
    for (String fName: textureTitleList){
        textureMap.put(fName, assetManager.get("markers/" + fName, Texture.class));
    }
}

@Override
public void render() {

    if (!isLoaded && assetManager.update()) {
        loadModel();
        isLoaded = true;
    }
    ...
}

The problem will be arisen on using the marker for these objects which causes a huge amount of memory usage. Loading a sphere model for each point objects takes about 14M of graphics memory but loading texture markers take 500M of memory on the device. I’ve used png icons located in the asset folders to create my textures but not using a Libgdx atlas. How can I prevent the memory leak and create this number of textures?

32

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
Awes0meM4ncommented, Jan 3, 2020

@NasserTahani I think that your problem is that you are loading the same texture for each marker readed. You have to load the texture once and reference that one on each marker (accordind with your example picture). This way only use the ram for one. Even you can color a texture for selected one.

0reactions
Awes0meM4ncommented, Jan 5, 2020

In create() you have to load texture only once, not one per marker. Then you fetch the texture like in the wiki. When you have your texture (or your array of texture required for your LabelActor constructor), you can instantiate all the markers you need using the same texture (improving also the draw performance)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android Libgdx memory leak on creating Actors - Stack Overflow
To do that I've extended from Actor and in this class, I've created two textures representing the marker in normal and the selected...
Read more >
Memory management - libGDX
Failure to dispose resources will lead to severe memory leaks! The following classes need to be disposed of manually (might not be complete, ......
Read more >
Memory leak while loading libgdx game screen
So, I have a lot of texture atlases in my code for a talking tom type libgdx, and I use around 6 of...
Read more >
Common Mistakes - OpenGL Wiki - Khronos Group
This problem usually manifests itself with constructors, when a user creates a texture object or similar OpenGL object wrapper at global scope.
Read more >
The LibGDX performance guide - Yair Morgenstern - Medium
You may see that the allocated memory, and the memory dumps, don't contain the entirety of the memory used by your app. This...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found