我的应用程序有一些问题。有时它会滞后并给我“释放 GC_CONCURRENT”。我使用 MAT 查看是什么消耗了这么多内存,我发现一个对象列表占用了大量内存。问题是我的游戏中有障碍物,我需要查看我的玩家是否踩到它们,这就是我使用此列表的原因。我目前有 200 个 block ,但我会有更多,我认为它们不应该使用这么多内存,我该怎么做才能解决这个问题?这是 block 类的样子:
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;
}
}
最佳答案
GC_CONCURRENT freed
表示垃圾收集器被调用,因为对象被杀死(例如,将 null
分配给它们)。
有一个概念叫Object Pooling ,它会重用死对象而不是杀死它,并从池中获取对象而不是创建新对象,因此您没有 GC 调用,同样也没有释放 GC_CONCURRENT
。
关于Android Studio Libgdx 内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32208654/