fix memory leak
Some checks failed
studiorailgun/Renderer/pipeline/head There was a failure building this commit

This commit is contained in:
austin 2024-12-07 15:37:20 -05:00
parent 01a2ab603c
commit ab5e65b14f
4 changed files with 35 additions and 9 deletions

View File

@ -1267,6 +1267,7 @@ Fluid chunk terrain bounds transfer
Cellular transfer behavior work Cellular transfer behavior work
Native math utils Native math utils
Frame tracking on native side Frame tracking on native side
Fix memory leak in client fluid data chunks
# TODO # TODO

View File

@ -2,8 +2,8 @@ package electrosphere.client.fluid.cache;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
/** /**
@ -16,7 +16,12 @@ public class ClientFluidCache {
//the map of chunk key -> chunk data //the map of chunk key -> chunk data
Map<String,FluidChunkData> cacheMap = new HashMap<String,FluidChunkData>(); Map<String,FluidChunkData> cacheMap = new HashMap<String,FluidChunkData>();
//the list of keys in the cache //the list of keys in the cache
List<String> cacheList = new LinkedList<String>(); LinkedList<String> cacheList = new LinkedList<String>();
/**
* Lock for the fluid cache to threadsafe
*/
ReentrantLock lock = new ReentrantLock();
/** /**
* Constructor * Constructor
@ -34,12 +39,21 @@ public class ClientFluidCache {
* @param chunkData The chunk data to add at the specified positions * @param chunkData The chunk data to add at the specified positions
*/ */
public void addChunkDataToCache(int worldX, int worldY, int worldZ, FluidChunkData chunkData){ public void addChunkDataToCache(int worldX, int worldY, int worldZ, FluidChunkData chunkData){
cacheMap.put(getKey(worldX,worldY,worldZ),chunkData); lock.lock();
String key = this.getKey(worldX,worldY,worldZ);
if(cacheMap.containsKey(key)){
FluidChunkData currentChunk = cacheMap.remove(key);
cacheList.remove(key);
currentChunk.freeBuffers();
}
cacheMap.put(key,chunkData);
cacheList.add(key);
while(cacheList.size() > cacheSize){ while(cacheList.size() > cacheSize){
String currentKey = cacheList.remove(0); String currentKey = cacheList.pop();
FluidChunkData currentChunk = cacheMap.remove(currentKey); FluidChunkData currentChunk = cacheMap.remove(currentKey);
currentChunk.freeBuffers(); currentChunk.freeBuffers();
} }
lock.unlock();
} }
@ -62,7 +76,10 @@ public class ClientFluidCache {
* @return True if the cache contains chunk data at the specified point, false otherwise * @return True if the cache contains chunk data at the specified point, false otherwise
*/ */
public boolean containsChunkDataAtWorldPoint(int worldX, int worldY, int worldZ){ public boolean containsChunkDataAtWorldPoint(int worldX, int worldY, int worldZ){
return cacheMap.containsKey(getKey(worldX,worldY,worldZ)); lock.lock();
boolean rVal = cacheMap.containsKey(getKey(worldX,worldY,worldZ));
lock.unlock();
return rVal;
} }
@ -77,7 +94,10 @@ public class ClientFluidCache {
* @return The chunk data if it exists, null otherwise * @return The chunk data if it exists, null otherwise
*/ */
public FluidChunkData getSubChunkDataAtPoint(int worldX, int worldY, int worldZ){ public FluidChunkData getSubChunkDataAtPoint(int worldX, int worldY, int worldZ){
return cacheMap.get(getKey(worldX,worldY,worldZ)); lock.lock();
FluidChunkData rVal = cacheMap.get(getKey(worldX,worldY,worldZ));
lock.unlock();
return rVal;
} }
} }

View File

@ -5,6 +5,7 @@ import java.nio.ByteOrder;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import org.joml.Vector3i; import org.joml.Vector3i;
import org.lwjgl.BufferUtils;
import electrosphere.server.fluid.manager.ServerFluidChunk; import electrosphere.server.fluid.manager.ServerFluidChunk;
@ -180,14 +181,18 @@ public class FluidChunkData {
* Frees the buffers contained within this chunk * Frees the buffers contained within this chunk
*/ */
public void freeBuffers(){ public void freeBuffers(){
this.free(); // this.free();
} }
/** /**
* Allocates the buffers for this data * Allocates the buffers for this data
*/ */
public void allocateBuffs(){ public void allocateBuffs(){
this.allocate(); // this.allocate();
this.bWeights = BufferUtils.createByteBuffer(CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE * 4);
this.bVelocityX = BufferUtils.createByteBuffer(CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE * 4);
this.bVelocityY = BufferUtils.createByteBuffer(CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE * 4);
this.bVelocityZ = BufferUtils.createByteBuffer(CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE * 4);
//reorder //reorder
this.bWeights.order(ByteOrder.LITTLE_ENDIAN); this.bWeights.order(ByteOrder.LITTLE_ENDIAN);

View File

@ -34,7 +34,7 @@ public class ClientFluidManager {
public static final int INTERPOLATION_RATIO = ServerTerrainManager.SERVER_TERRAIN_MANAGER_INTERPOLATION_RATIO; public static final int INTERPOLATION_RATIO = ServerTerrainManager.SERVER_TERRAIN_MANAGER_INTERPOLATION_RATIO;
//caches chunks from server //caches chunks from server
static final int CACHE_SIZE = 50; static final int CACHE_SIZE = 250;
//used for caching the macro values //used for caching the macro values
ClientFluidCache fluidCache; ClientFluidCache fluidCache;