EditableMesh Memory Leak

I’m currently (as of 5th March) experiencing a nasty memory leak with EditableMeshes.

Here is a video demonstration (please note that this video is sped up and a lot of editable meshes were generated making the issue seem more exacerbated).


Reproduction Steps:

Run This Script:

--!strict

local RunService = game:GetService("RunService")

local CHUNK_SIZE, CHUNK_SCALE = 100, 5
local SCALED_CHUNK_SIZE = CHUNK_SIZE - 1
local CHUNK_BOUNDS_X = (CHUNK_SIZE ^ 2) - CHUNK_SIZE

local RENDER_DIST = 25

local function CreateChunk(xOffset: number, zOffset: number)
	local eMesh = Instance.new("EditableMesh")
	
	local vertices = table.create(CHUNK_SIZE * 2)
	
	local iter = 1
	for x = 1, CHUNK_SIZE do
		for z = 1, CHUNK_SIZE do
			local scaledX, scaledZ = x + (xOffset * SCALED_CHUNK_SIZE), z + (zOffset * SCALED_CHUNK_SIZE)
			local vertPos = Vector3.new(scaledX, math.noise(scaledX / 16, scaledZ / 16) * 10, scaledZ) * CHUNK_SCALE
			vertices[iter] = eMesh:AddVertex(vertPos)
			iter += 1
		end
	end
	
	for iter = 1, CHUNK_SIZE ^ 2 do
		if (iter % CHUNK_SIZE == 0) or (iter >= CHUNK_BOUNDS_X) then continue end
		
		local vertA, vertB, vertC, vertD =
			vertices[iter], vertices[iter + CHUNK_SIZE],
			vertices[iter + CHUNK_SIZE + 1], vertices[iter + 1]

		eMesh:AddTriangle(vertC, vertB, vertA)
		eMesh:AddTriangle(vertD, vertC, vertA)
	end
	
	local mesh = Instance.new("MeshPart")
	mesh.Anchored = true
	mesh.Size = Vector3.one
	eMesh.Parent = mesh
	mesh.Parent = workspace

	return mesh :: MeshPart & { EditableMesh: EditableMesh }
end

local chunks: { MeshPart & { EditableMesh: EditableMesh } } = table.create(RENDER_DIST ^ 2)

while true do
	task.wait(5)
	print("generating")
	
	local iter = 1
	for x = 1, RENDER_DIST do
		for z = 1, RENDER_DIST do
			chunks[iter] = CreateChunk(x, z)
			iter += 1
			if iter % 20 == 0 then RunService.Stepped:Wait() end
		end
	end
	
	task.wait(1)
	
	print("destroying")
	
	for chunkIdx, chunk in chunks do
		chunks[chunkIdx] = nil
		chunk.EditableMesh:Destroy()
		chunk:Destroy()
		if iter % 5 == 0 then RunService.Stepped:Wait() end
	end
	
	task.wait(1)
end

This script repetitively generates and deletes a ton of EditableMeshes. (this was also the script used in the video demonstration above).

4 Likes

Have you tried manually deleting every vertex and surface inside the mesh prior to destroying it?

1 Like

Yes i have tried this and the issue still occurs sadly.

We’ve filed a ticket to our internal database and will update you as soon as we have news!

Thanks for flagging!

3 Likes

Hey, I’m wondering if you could upload the video again or share it with me through message? I’m trying to reproduce and debug the memory leak. Thanks a lot!

3 Likes

Hello, here is the video.

1 Like