在我的android游戏中,我将整个游戏场景/关卡都包装在一个GameScene对象中。我希望一旦引用被新的GameScene替换,GameScene对象就会进行垃圾回收。不幸的是,我有严重的内存泄漏,每次更改级别时,内存堆都会增长。这种情况会一直发生,直到游戏崩溃并出现内存已满错误。我已经用过eclipse Mat了,它警告我GameScene中可能有一个漏洞。我修复了它,所以它不会再出现在Mat上了。有没有擅长在eclipse中调试的人知道我如何找出哪些对象没有被垃圾回收?我甚至不确定GameScene是否会被垃圾回收。下面的代码只是给出了一个简短的示例,展示了我如何调用Engine来创建并覆盖正在运行的GameScene中的当前GameScene,从而创建下一个级别。
class GameScene{
//lots of methods and objects...
@Override
public void onClick(float x, float y) {
// TODO Auto-generated method stub
Engine.destroyScene();
Engine.setScene(new GameScene(context, "chapters/classic/2/"));
}
}
class SceneManager{
public Scene currentScene;
public void destroyScene() {
sceneLoaded = false;
currentScene.destroyScene();
currentScene = null;
}
public void setScene(Scene scene) {
currentScene = scene;
}
public void loadScene() {
currentScene.loadScene();
sceneLoaded = true;
}
}
class Engine{
public static void destroyScene(){
sceneManager.destroyScene();
resourceManager.deleteAllTextures();
}
public static void setScene(Scene scene) {
sceneManager.setScene(scene);
}
}发布于 2014-11-14 10:25:05
您可以使用诸如VisualVm (包含在JDK中)这样的分析器来查看内存的状态、每个对象的实例数量以及其他有用的信息。
在你的例子中,我会在一开始对内存进行快照,然后在游戏场景中拍摄另一张快照,并与第一张快照进行比较。VisualVM提供的另一个选择是在发生OutOfMemoryException时创建堆转储,然后您可以通过堆转储来查找问题的根源。
https://stackoverflow.com/questions/26921761
复制相似问题