Anaminus / roblox-bug-tracker

Formerly an unofficial bug tracker for Roblox.
31 stars 24 forks source link

Memory Leak #149

Closed Quenty closed 10 years ago

Quenty commented 11 years ago

The following code creates a memory leak in a player's game, if ran local, that memory leaks and uses up their core's memory unless they've limited Lua's memory.

 while wait() do
      for a = 1, math.huge do
           delay(0, function() return end) -- Key here.
      end
 end

Unlike most crash scripts, this can actually crash a single core computer, as Mark Otaris can testify.

chc4 commented 11 years ago

This has to do with Roblox not collecting dead Lua threads. I've also noticed this when using Delay-intensive scripts in Script Builders.

Quenty commented 11 years ago

Yeah, also threads are just resource intensive. I switched from a thread to queue based system in a script and went from 58% activity to 14% activity.

matthewdean commented 10 years ago

This can be achieved with a single loop. Here is some code which better illustrates the point:

while true do
  Spawn(function() print("new thread") end)
end
print("old thread")

Sample output:

old thread
new thread
new thread
...

The fact that old thread is always printed first indicates that calling a function such as Spawn and Delay will not cause the calling thread to yield. This means the spawned threads have no chance to execute and get garbage collected until you are done looping. If you want the new threads to start running, you should put a wait inside the loop:

while true do
  Spawn(function() print("new thread") end)
  wait()
end

In short, this "bug" is simply a misunderstanding of how RBX.Lua threads work and is not anything that needs to be fixed.