Memory leak after clone modulescript

Script memory continuous going up when clone ModuleScript even though get destroyed. Anyone know why?
Test code below:

while true do
	for i = 1, 1000, 1 do
		local m = Instance.new("ModuleScript")
		m.Parent = workspace.Tmp
		m:Destroy()
	end
	task.wait(1)
end

results:
PlaceMemory->Script 270MB->1360MB


PlaceMemory->Signal 20MB->420MB

CoreMemory->default 200MB->1895MB

2 Likes

Why are you cloning a module script. I thought there entire point was so that they don’t need to be cloned?

For example:

  1. I have a modulescript called “EnemyControl”, it will be cloned everytime when an enemy spawned
  2. I also have a modulescript called “Bullet”, it will be cloned and parent to the bullet module when player shoot
1 Like
while true do
	for i = 1, 1000, 1 do
		local m = Instance.new("ModuleScript")
		m.Parent = workspace.Tmp
		m:Destroy()
		print(m)
	end
	task.wait(1)
end

The command print(m) will print an object, not nil. This means that the module scripts after being destroyed are still stored somewhere in memory.

1 Like

:scream:Is it an roblox engine bug? or it is designed to be that?

1 Like

I don’t think that it’s a bug. Destroy has been on Roblox for many years and the “issue” is still here. This is what the official API has to say about it:

Sets the Instance.Parent property to nil, locks the Instance.Parent property, disconnects all connections, and calls Destroy on all children. This function is the correct way to dispose of objects that are no longer required. Disposing of unneeded objects is important, since unnecessary objects and connections in a place use up memory (this is called a memory leak) which can lead to serious performance issues over time.

In conclusion, this is technically intentional to work like that.

Then it seems like roblox not recommend to clone a modulescript or script in game :thinking:
And I am realy confused that each player has serveral official script running under it like Health or PlayerControl. Do they cause memory leak when player get out?

Roblox servers don’t last forever. There is always something to make servers in Your game restart, like a Roblox Client update. I don’t think that someone needs to make 1000 scripts every second. Just work on the game like normal and it won’t be that problematic (I hope).

My game spawn enemies on the server, and each enemy has a controller ModuleScript under it. Our server will reach 6GB memory and crash every 13 hours :smiling_face_with_tear:

I think that servers are a lot more powerful than clients devices so i think that a server for Your game will be able to work properly for way longer than 13 hours. You can also use this tip from the official API:

Tip: After calling Destroy on an object, set any variables referencing the object (or its descendants) to nil. This prevents your code from accessing anything to do with the object.

This will also reduce memory usage because server needs less space to store nil comparing to let’s say a module script object.

1 Like

I have tried this but won’t help :smiling_face_with_tear:

while true do
	for i = 1, 1000, 1 do
		local m = Instance.new("ModuleScript")
		m.Parent = workspace.Tmp
		m:Destroy()
                m = nil
	end
	task.wait(1)
end
1 Like