Skip to content

Commit

Permalink
Fix block entity memory leak (#3689)
Browse files Browse the repository at this point in the history
* Fix block entity memory leak

* Add check of empty time

* Move inject point

* Bump SpongeAPI
  • Loading branch information
Lignium committed Aug 22, 2022
1 parent 11424e2 commit 3bf6cb2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
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()) {
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

0 comments on commit 3bf6cb2

Please sign in to comment.