Prevent memory leak when unloading chunks

This commit is contained in:
Felix Cravic 2020-05-15 15:38:06 +02:00
parent 4a287f82a6
commit ee90f82969
2 changed files with 28 additions and 0 deletions

View File

@ -62,6 +62,7 @@ public class Chunk implements Viewable {
private Set<Integer> blockEntities = new CopyOnWriteArraySet<>();
// Cache
private boolean loaded = true;
private Set<Player> viewers = new CopyOnWriteArraySet<>();
private ByteBuf fullDataPacket;
@ -356,6 +357,16 @@ public class Chunk implements Viewable {
});
}
/**
* Used to verify if the chunk should still be kept in memory
* having the chunk unloaded means no data is contained in it (blocks, data, etc...)
*
* @return true if the chunk is loaded
*/
public boolean isLoaded() {
return loaded;
}
@Override
public String toString() {
return "Chunk[" + chunkX + ":" + chunkZ + "]";
@ -384,6 +395,22 @@ public class Chunk implements Viewable {
return Collections.unmodifiableSet(viewers);
}
/**
* Used to prevent major memory leak when unloading chunks
*/
protected void unload() {
this.loaded = false;
this.biomes = null;
this.blocksId = null;
this.customBlocksId = null;
this.blocksData.clear();
this.updatableBlocks.clear();
this.updatableBlocksLastUpdate.clear();
this.blockEntities.clear();
this.viewers.clear();
}
private int getBlockIndex(int x, int y, int z) {
x = x % Chunk.CHUNK_SIZE_X;
z = z % Chunk.CHUNK_SIZE_Z;

View File

@ -287,6 +287,7 @@ public class InstanceContainer extends Instance {
}
this.chunks.remove(index);
chunk.unload();
}
@Override