爲什麼堆沒有理由上漲?如果你說使用gc,我仍然想知道爲什麼要上去?爲什麼Libgdx不斷增加堆?
這裏是我的代碼:
public class GameCore extends ApplicationAdapter {
SpriteBatch batch;
Texture texture;
StopWatch sw = new StopWatch();
long current, held;
@Override
public void create() {
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("badlogic.jpg"));
sw.start();
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(texture, 0, 0);
batch.end();
if (Gdx.app.getJavaHeap() > held) {
current = Gdx.app.getJavaHeap();
held = current;
String f = "UP: " + Mis.formatMilliseconds(sw.getCurrent()) + "\t"
+ Mis.ramStatistics(Mis.BYTE_TO_MB_FACTOR, true) + "\t, byGdx:" + Gdx.app.getJavaHeap();
System.out.println(f);
} else if (Gdx.app.getJavaHeap() < held) {
// if gc worked
held = current;
String f = "DOWN: " + Mis.formatMilliseconds(sw.getCurrent()) + "\t"
+ Mis.ramStatistics(Mis.BYTE_TO_MB_FACTOR, true) + "\t, byGdx:" + Gdx.app.getJavaHeap();
System.out.println(f);
}
}
@Override
public void dispose() {
batch.dispose();
}
}
不幸的結果:
UP: 1 ms Heap: (11/1796) MB, 0.59153545% , byGdx:11140048
UP: 58 sec, 58171 ms Heap: (12/1796) MB, 0.62772965% , byGdx:11821672
UP: 1 min, 111 sec, 111705 ms Heap: (12/1796) MB, 0.66392213% , byGdx:12503264
UP: 32 min, 1978 sec, 1978210 ms Heap: (25/1796) MB, 1.3516115% , byGdx:25454120
UP: 48 min, 2887 sec, 2887645 ms Heap: (31/1796) MB, 1.6773589% , byGdx:31588736
增加(最高可達34MB)的分鐘後56,GC清理(26MB),libgdx再次啓動沒有特殊原因增加堆大小。
是什麼促使我問這個問題是一個非常小的遊戲只有2個紋理,libgdx在2分鐘內導致了66mb,我以爲我錯誤地使用了libgdx,但我不是。
你是否經常將物體加載到內存中?即每幀加載圖像? –
不,我不是,因爲除了那裏,你什麼都看不到。 – user3843006