Never Mind
Never Mind

Reputation: 297

Android Studio Libgdx Memory problems

I have some problems with my app. Sometimes it lags and gives me "GC_CONCURRENT freed". I use MAT to look what consumes so much memory and I found out that a list of objects eats a lot of memory. The problem is that I have blocks in my game and I need to see if my player steps on them so that's why I am using this list. I currently have 200 blocks, but I will have much more and I don't think they should use so much memory, what can I do to fix the problem? This is how the block class looks:

package com.NeverMind.DontFall.android;

import android.util.Log;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

/**
 * Created by user on 20-Aug-15.
 */
public class blockClass {
    private SpriteBatch spriteBatch;
    public float x, y, sizeX = 100, sizeY = 100, startTime;
    private boolean isTouched = false;
    private Texture texture;
    Sprite blockSprite;
    public blockClass(float sentX, float sentY, Texture sentTexture, float scaleX, float scaleY){
        x = sentX;
        y = sentY;
        sizeY *= scaleY;
        sizeX *= scaleX;
        spriteBatch = new SpriteBatch();
        texture = sentTexture;
        blockSprite = new Sprite(texture);
        startTime = System.currentTimeMillis();
    }
    public void draw(float cameraX, float cameraY) {
        spriteBatch.begin();
        spriteBatch.draw(blockSprite, x + cameraX, y + cameraY, sizeX, sizeY);
        spriteBatch.end();
    }
    public void update(float posX, float posY, boolean immune){
        if (isTouched == false && immune == false)
            if (touched(posX, posY) )
                isTouched = true;
        if (isTouched == true) {
            y -= 10;
        }
    }
    public boolean touched (float posX, float posY)
    {
        if (posX >= x && posX < x + sizeX && posY == y + sizeY)
            return true;
        return false;
    }
    public boolean toKill (float posY){
        if (isTouched && y < posY - 1000)
            return true;
        return false;
    }
}

Upvotes: 3

Views: 219

Answers (1)

Saeed Masoumi
Saeed Masoumi

Reputation: 8916

GC_CONCURRENT freed means that Garbage Collector is called, because of killing objects (e.g assign null to them).

There is a concept calls Object Pooling, it reuses dead object instead of kill it and obtain object from pool instead of creating a new one, So you have no GC calls and similarly no GC_CONCURRENT freed.

Upvotes: 5

Related Questions