mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-01 05:58:00 +01:00
Prevent memory leak when unloading chunks
This commit is contained in:
parent
4a287f82a6
commit
ee90f82969
@ -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;
|
||||
|
@ -287,6 +287,7 @@ public class InstanceContainer extends Instance {
|
||||
}
|
||||
|
||||
this.chunks.remove(index);
|
||||
chunk.unload();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user