So week ago i discovered that Roblox’s Luau implements memory optimization that may cause a lot of lag in my game
Details
After few experiments, it turns out that when you nulify index or hash of a table, very small amount of memory is left, it’s special optimization because if we doesn’t destroy table, lua thinks that we want to re-use it in the future, and this is very good optimization
Problem is that if someone can’t clear table because it’s important to game it may cause memory increase over time, and i can’t do anything about it
If you’ve read details and have any ideas, thank you
Two things can cause significant lag: memory leaks as you mentioned, too many event listeners (things bound to Events), and too many threads, I.e. task.spawn()'s that don’t ever return. To catch the latter two, I would recommend wrapping these actions in a function so that you can selectively add measurements to them. You can measure things like how long threads are alive for, how many threads there are over time, etc.
Memory leaks only occur when an object is still possible to reference but you didn’t intend for it to be that way. Instrumenting this is very involved and I don’t know a good way to do it. This is an easy mistake to make if you have multiple scripts referring to instances, so I’d check there first. I believe Lua’s garbage collector will not be fooled by circular references.
If there was a memory leak in the Lua implementation you’d be out of luck, but that’s extremely unlikely. You probably have some code that allocates memory but never allows it to be dropped. The simplest possibility is you have a table somewhere that still refers to it.
I made 10 hours of experiments and then desperately looked at forums and chat gpt, it turned out that this is optimization at machine level to reduce allocation/deallocation stress, making it possible for lower-end devices to use lua as lightweight
Here it is, this is feature that causes very small but still visible memory leak, especially if you work on a large tables very often
EDIT: forget to mention that this only happens to module scripts, other tables can be GCed because they go out of scope, module scripts cannot be because if we require them they simply have reference all the time, and are ment for re-usability
Let me just level with you and say I’m trying to make sure you’re not engaging in voodoo programming. One form of this is imagining a problem that doesn’t exist, but is out of your control, to answer why you can’t figure something out. I don’t believe you when you say theres a feature in Lua that causes unavoidable memory leaks. i don’t believe that you read this anywhere, or if you did you’ve misunderstood something, but you won’t tell me where you got it, so I have no idea. Memory leaks have a lot of causes and it is possible for them to be mistakes in the language. Leaks also have to exist in order to follow the rules of the language for certain programs; these are the ones you have to manually avoid, it’s not possible to design them out of the language without making that language weaker.
Ok, it’s not mentioned anywhere, why? because it’s how module scripts behave in Roblox
I mentioned i made a lot of experiments, not only with mine but also other systems and this behavior occured there too
It turns out that module scripts keep reference from nulified hashes or indexes and can reuse their memory allocations later, which might cause some memory being left until you nulify module script or set it to empty table, it’s not something big in normal cases, but i want to add building to my game and player may place thousand of OOP objects, but when he destroys them small amount of memory is still here, and i can’t simply nulify module which stores those objects