Help! Huge memory leak (10+GB) in my code!

I’m trying to make a 3d cellular automata cave generation script. What I have been recently trying to do was make it work with chunk loading so that I can then set it up with rendering so it is infinite. However, for some reason when doing this, roblox increases memory usage by ~50MB every time I run the game and will stay at that usage until I restart studio. I have NO idea what’s going on here, so it would be really helpful if somebody could help me. This was not happening when I just did a singular 50x50x50 cube, so I don’t see why a 48x48x48 cube causes 10GB of memory to be used

math.randomseed(tick())

local scale = 4
local size = 16
local offset = Vector3.new(0, 0, 0)

local aliveChance = 0.45
local birthLimit = 12
local deathLimit = 11
local stepAmount = 4

-- Generates a 3d grid based on certain dimensions
function appendChunkToGrid(grid, chunk)
	local chunkX = chunk.X * size
	local chunkY = chunk.Y * size
	local chunkZ = chunk.Z * size
	for i = chunkX, size + chunkX do
		if not grid[i] then
			grid[i] = grid[i] or {}			
		end
		for j = chunkY, size + chunkY do
			if not grid[i][j] then
				grid[i][j] = grid[i][j] or {}
			end
			for k = chunkZ, size + chunkZ do
				if math.random() <= aliveChance then
					grid[i][j][k] = "O"
				else
					grid[i][j][k] = "X"
				end
			end
		end
	end
	print("on append step "..collectgarbage("count") * 1024)
end
-- Function to print the grid for troubleshooting
function printGrid(grid)
	for i, v in pairs(grid) do
		for j, k in pairs(v) do
			local printString = table.concat(k, "")
			print(printString)
		end
	end
end
-- Function to count neighbors in a 2d array
function countNeighbors(grid, xPos, yPos, zPos)
	local neighbors = 0

	for x = -1, 1 do
		for y = -1, 1 do
			for z = -1, 1 do
				if grid[xPos + x] then
					if grid[xPos + x][yPos + y] then
						if grid[xPos + x][yPos + y][zPos + z] then
							local neighbor = grid[xPos + x][yPos + y][zPos + z]
							if neighbor == "O" then
								neighbors += 1
							end
						end
					end
				end
			end
		end
	end
	return neighbors
end
-- Function to perform a step of cellular automata
function doSimulationStep(grid, chunk)
	local offsetX = chunk.X * size
	local offsetY = chunk.Y * size
	local offsetZ = chunk.Z * size
	local newGrid = grid
	for x = offsetX, offsetX + size do
		for y = offsetY, offsetY + size do
			for z = offsetZ, offsetZ + size do
				local neighbors = countNeighbors(grid, x, y, z)
				if grid[x][y][z] == "O" then
					if neighbors < deathLimit then
						newGrid[x][y][z] = "X"
					else
						newGrid[x][y][z] = "O"
					end
				else
					if neighbors > birthLimit then
						newGrid[x][y][z] = "O"
					else
						newGrid[x][y][z] = "X"
					end
				end
			end
		end
	end
	print("on simulation step: "..collectgarbage("count") * 1024)
	return newGrid
end
-- Function to place grid into workspace
function placeGrid(grid, chunk)
	local offsetX = chunk.X * size
	local offsetY = chunk.Y * size
	local offsetZ = chunk.Z * size
	for x = offsetX, offsetX + size do
		for y = offsetY, offsetY + size do
			for z = offsetZ, offsetZ + size do
				if grid[x][y][z] == "O" and countNeighbors(grid, x, y, z) < 26 then
					local part = Instance.new("Part")
					part.Size = Vector3.new(1, 1, 1) * scale
					part.Position = (Vector3.new(x, y, z) + offset) * scale
					part.Anchored = true
					part.Parent = workspace.CaveParts
				end
			end
		end
		wait()
	end
	print("on place step: "..collectgarbage("count") * 1024)
end

local grid = {}

for i = 0, 2 do
	for j = 0, 2 do
		for k = 0, 2 do
			local chunk = Vector3.new(i, j, k)
			appendChunkToGrid(grid, chunk)
			print(grid)
			for i = 1, stepAmount do
				grid = doSimulationStep(grid, chunk)
			end
			placeGrid(grid, chunk)
			wait()
		end
	end
end

3 Likes