Box2d内存泄漏

时间:2016-08-27 01:06:29

标签: java android libgdx box2d

我已经确定box2d导致内存泄漏。仅仅20分钟后,它占用了10%的应用程序内存,一直到50%。游戏开始时的对象数为100,000。这个数字(即使在GC之后)上升到150万以上。该应用程序不仅在Android上速度变慢,而且桌面开始运行也很糟糕。最初运行良好,但5分钟后开始看起来很迟钝。我将链接下面的相关代码。但首先,这里有更多信息:

我需要动态更改这些物体的灯具尺寸。所以我的解决方案是每帧破坏/重新创建适当大小的灯具。使用box2d的共有8个对象(与下面的代码完全相同)。这些对象是我认为唯一潜在的泄漏点。

对于前面提到的8个敌人对象,每个帧都调用下面的方法。我还应该说每次调用都会将值'true'传递给此方法。正如您将看到的,我正在处理灯具和形状,因此我不确定如何发生内存泄漏。

private void createFixtures(boolean remove) {

    if (remove) {
        if (fixture_inner != null) {
            if(body.getFixtureList().contains(fixture_inner, false)) {
                body.destroyFixture(fixture_inner);
            }
        }
        if (fixture_outer != null) {
            if(body.getFixtureList().contains(fixture_outer, false)) {
                body.destroyFixture(fixture_outer);
            }
        }
        if (fixture_gap != null) {
            if(body.getFixtureList().contains(fixture_gap, false)) {
                body.destroyFixture(fixture_gap);
            }
        }
    }

    if (getInnerBounds() != null) {
        FixtureDef fixtureDef_inner = new FixtureDef();
        PolygonShape shape_inner = new PolygonShape();
        shape_inner.set(getInnerBounds().getTransformedVertices());
        fixtureDef_inner.shape = shape_inner;
        fixtureDef_inner.filter.categoryBits = Config.CATEGORYBIT_BLOCKER;
        fixtureDef_inner.filter.maskBits = Config.CATEGORYBIT_PLAYER;
        fixtureDef_inner.isSensor = true;
        fixture_inner = body.createFixture(fixtureDef_inner);
        BlockerFixtureUserData fixtureUserData_inner = new BlockerFixtureUserData(this, gameObjects.indexOf(this, false));
        fixture_inner.setUserData(fixtureUserData_inner);

        shape_inner.dispose();
    }

    if (getOuterBounds() != null) {
        FixtureDef fixtureDef_outer = new FixtureDef();
        PolygonShape shape_outer = new PolygonShape();
        shape_outer.set(getOuterBounds().getTransformedVertices());
        fixtureDef_outer.shape = shape_outer;
        fixtureDef_outer.filter.categoryBits = Config.CATEGORYBIT_BLOCKER;
        fixtureDef_outer.filter.maskBits = Config.CATEGORYBIT_PLAYER;
        fixtureDef_outer.isSensor = true;
        fixture_outer = body.createFixture(fixtureDef_outer);
        BlockerFixtureUserData fixtureUserData_outer = new BlockerFixtureUserData(this, gameObjects.indexOf(this, false));
        fixture_outer.setUserData(fixtureUserData_outer);

        shape_outer.dispose();
    }

    if (getGapBounds() != null) {
        FixtureDef fixtureDef_gap = new FixtureDef();
        PolygonShape shape_gap = new PolygonShape();
        shape_gap.set(getGapBounds().getTransformedVertices());
        fixtureDef_gap.shape = shape_gap;
        fixtureDef_gap.filter.categoryBits = Config.CATEGORYBIT_BLOCKERGAP;
        fixtureDef_gap.filter.maskBits = Config.CATEGORYBIT_PLAYER;
        fixtureDef_gap.isSensor = true;
        fixture_gap = body.createFixture(fixtureDef_gap);
        BlockerFixtureUserData fixtureUserData_gap = new BlockerFixtureUserData(this, gameObjects.indexOf(this, false));
        fixture_gap.setUserData(fixtureUserData_gap);

        shape_gap.dispose();
    }
}

玩家也有类似的方法,只为玩家指定相同的方法。

 private void createFixture(boolean remove) {

    if (remove) {
        body.destroyFixture(fixture);
    }

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(0, 0);
    body = world.createBody(bodyDef);
    FixtureDef fixtureDef = new FixtureDef();
    CircleShape circle = new CircleShape();
    circle.setRadius(getWidth() / 2);
    circle.setPosition(new Vector2(0, getY() + getHeight() / 2));
    fixtureDef.shape = circle;
    fixtureDef.filter.categoryBits = Config.CATEGORYBIT_PLAYER;
    fixture = body.createFixture(fixtureDef);

    circle.dispose();
}

处理世界是我的主要游戏类。但重要的是要注意(也许?)此游戏目前只有1个屏幕。

如果您可能需要任何其他信息,请随时提出!太感谢了!

1 个答案:

答案 0 :(得分:0)

我的问题是我在播放器createFixture方法中实例化一个体,每个帧都调用该体来编辑夹具大小。我把它移到了构造函数中。