gpt4 book ai didi

java - Box2d 内存泄漏

转载 作者:太空宇宙 更新时间:2023-11-04 12:17:58 27 4
gpt4 key购买 nike

我已确定 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 个屏幕。

如果您需要任何其他信息,请随时询问!太感谢了!

最佳答案

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

关于java - Box2d 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39176537/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com