在游戏关闭时配置libGDX资源?

时间:2015-10-08 16:30:28

标签: java android libgdx garbage-collection

我使用libGDX并在我不再需要它们时释放我的资源以释放RAM。游戏结束时我是否需要手动处理资源?或者这是由Android(或libGDX)自动完成的吗?

2 个答案:

答案 0 :(得分:1)

是的,您必须,或者与所有这些对象关联的本机内存都将泄漏。在Android中关闭活动(这是托管Libgdx游戏的活动)不会关闭整个应用程序,因此操作系统不会回收内存,您将丢失对可以处置的对象的所有引用。

答案 1 :(得分:0)

事实是,某些物体需要手动处理 - 例如:参考它们是

  • 像素图
  • 纹理
  • 位图

和其他许多人you will find here

为了便于管理您的资产,您应该使用AssetManager。如何使用is desribed here的方式,但一般模式是:

  1. 创建 AssetManager 实例
  2. 将资源加载到 AssetManager 实例
  3. 使用从提到的对象获取资源的资源
  4. 在应用程序生命周期结束时处置 AssetManager 实例
  5. 示例代码:

        AssetManager assetManager = new AssetManager();
    
        assetManager.load("data/mytexture.png", Texture.class);
    
        assetManager.finishLoading(); //loading resources to asset manager is asynchrous! you can also use .update() method - find it in the reference
    
        ...
    
        Texture tex = assetManager.get("data/mytexture.png", Texture.class); //use the asset manager to obtain texture
    
        ...
    
        assetManager.clear(); //disposing asset manager with all its resources - you can also use .dispose() method