Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix block entity memory leak #3689

Merged
merged 4 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SpongeAPI
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import net.minecraft.world.level.storage.WorldData;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
import org.objectweb.asm.Opcodes;
import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockSnapshot;
Expand Down Expand Up @@ -128,6 +129,7 @@ public abstract class ServerLevelMixin extends LevelMixin implements ServerLevel

// @formatter:off
@Shadow @Final private ServerLevelData serverLevelData;
@Shadow private int emptyTime;

@Shadow @Nonnull public abstract MinecraftServer shadow$getServer();
@Shadow protected abstract void shadow$saveLevelData();
Expand Down Expand Up @@ -441,6 +443,25 @@ public void save(@Nullable final ProgressListener progress, final boolean flush,
this.impl$recentTickTimes[this.shadow$getServer().getTickCount() % 100] = postTickTime - this.impl$preTickTime;
}

@Inject(
method = "tick",
at = @At(
value = "FIELD",
target = "Lnet/minecraft/server/level/ServerLevel;emptyTime:I",
opcode = Opcodes.PUTFIELD,
shift = At.Shift.AFTER
)
)
private void impl$unloadBlockEntities(final BooleanSupplier param0, final CallbackInfo ci) {
// This code fixes block entity memory leak
// https://github.com/SpongePowered/Sponge/pull/3689
if (this.emptyTime >= 300 && !this.blockEntitiesToUnload.isEmpty()) {
Lignium marked this conversation as resolved.
Show resolved Hide resolved
this.tickableBlockEntities.removeAll(this.blockEntitiesToUnload);
this.blockEntityList.removeAll(this.blockEntitiesToUnload);
this.blockEntitiesToUnload.clear();
}
}

private void impl$setWorldOnBorder() {
((WorldBorderBridge) this.shadow$getWorldBorder()).bridge$setAssociatedWorld(this.bridge$getKey());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.spongepowered.common.util.DataUtil;
import org.spongepowered.math.vector.Vector3d;

import java.util.List;
import java.util.function.Predicate;

@Mixin(net.minecraft.world.level.Level.class)
Expand All @@ -77,6 +78,9 @@ public abstract class LevelMixin implements LevelBridge, LevelAccessor {
@Shadow protected float rainLevel;
@Shadow protected float oThunderLevel;
@Shadow protected float thunderLevel;
@Shadow @Final public List<BlockEntity> blockEntityList;
@Shadow @Final public List<BlockEntity> tickableBlockEntities;
@Shadow @Final protected List<BlockEntity> blockEntitiesToUnload;

@Shadow public abstract LevelData shadow$getLevelData();
@Shadow public abstract void shadow$updateSkyBrightness();
Expand Down