【问题标题】:Libgdx game out of memory and crashesLibgdx 游戏内存不足并崩溃
【发布时间】:2018-01-17 16:43:33
【问题描述】:

嘿,我做了一个小鱼游戏,在屏幕上移动,用户需要击打它们并杀死它们,2-3 分钟后应用程序完全终止,我不知道为什么,我只需创建一次对象并在屏幕上移动它们..

我收到的信息是

I/art: Background partial concurrent mark sweep GC freed 4771(2029KB) AllocSpace objects, 230(4MB) LOS objects, 39% free, 9MB/16MB, paused 3.054ms total 173.279ms

这里是一些代码: 图像数组的创建:

fishes = new Image[30];
    directionX = new int[30];
    directionY = new int[30];
    speed = new int[30];
    dead = new boolean[30];
    deadTimer = new int[30];


    Random random = new Random();
   Drawable drawable = new TextureRegionDrawable(new TextureRegion(new Texture("idag.png")));

    for(int i =0;i<fishes.length;i++)
    {
        int xlocation = random.nextInt(2);
        int ylocation = random.nextInt(2);
        dead[i] = false;
        deadTimer[i] = 0;

        fishes[i] = new Image(drawable);
        fishes[i].setWidth(character.getWidth()/3);
        fishes[i].setHeight(character.getWidth()/3);
        GenerateLocation(i);

        stage.addActor(fishes[i]);
    }

这里是每条鱼死亡时的位置

 public void GenerateLocation(int i) {
     random = new Random();

        int xlocation = random.nextInt(2);
        int ylocation = random.nextInt(2);
        if (xlocation == 0) {
            fishes[i].setX(-random.nextInt(8000));
            directionX[i] = 1;

        } else {
            fishes[i].setX(width + random.nextInt(8000));
            directionX[i] = -1;
        }

        if (ylocation == 0) {
            fishes[i].setY(random.nextInt((int) (height/2)));
            directionY[i] = 1;
        } else {


            fishes[i].setY(height-random.nextInt((int) (height/2)));
            directionY[i] = -1;
        }

        if (directionX[i] == 1 && directionY[i] == 1) {
            drawable = new TextureRegionDrawable(new TextureRegion(new Texture("idag-45.png")));
            fishes[i].setDrawable(drawable);
            //fishes[i].setRotation(-45);

        } else if (directionX[i] == 1 && directionY[i] == -1) {
            drawable = new TextureRegionDrawable(new TextureRegion(new Texture("idag-135.png")));
            fishes[i].setDrawable(drawable);
        } else if (directionX[i] == -1 && directionY[i] == 1) {
            drawable = new TextureRegionDrawable(new TextureRegion(new Texture("idag45.png")));
            fishes[i].setDrawable(drawable);
        } else if (directionX[i] == -1 && directionY[i] == -1) {
            drawable = new TextureRegionDrawable(new TextureRegion(new Texture("idag135.png")));
            fishes[i].setDrawable(drawable);
        }
        speed[i] = random.nextInt(levelSpeed) + 1;

}

谁能帮帮我?我真的不知道我还能做些什么来完成这项工作。

谢谢!

【问题讨论】:

    标签: android memory-management libgdx out-of-memory


    【解决方案1】:

    我在那里看到很多new Texture 调用,所以你正在生成很多对象。 LibGDX 中实现 Disposable 的任何内容都必须通过调用dispose() 进行处理,然后才能失去对它的引用。这是因为 Disposable 将数据保存在本机内存中,因此 GC 不会清理它。

    此外,一遍又一遍地加载相同的图像而不是重复使用相同的纹理实例是浪费时间。你的每条鱼都有另一个相同图像的副本,它们自己加载到内存中。

    【讨论】:

    • 好的,关于我修复的新纹理,我在构造函数中创建了 4 个可绘制对象,每当我想更改引用它们的图像时,游戏仍然崩溃,我什至尝试处理当我输了并且仍然崩溃的整个阶段,我真的不明白为什么。
    • 我也没有,因为我看不到您的代码或堆栈跟踪。 ;)
    猜你喜欢
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 2016-03-09
    相关资源
    最近更新 更多