What is garbage collecting and when should I manually do it and when is it automatic?

What is garbage collecting and when should I manually do it and when is it automatic?

EDIT: Also what should be garbage collected to prevent memory issues?

1 Like

i dont understand?
can you tell me a simple word or good meaning that your created this post

do you mean stuff like connections, threads, random parts that never got deleted?

A optimized script contains “garbage collecting” and I should implement that too.
My question is when (is it manual or automatic) and what should be garbage collected.
That way the game doesn’t have to suffer memory issues like leaks.

Yes, something like that. But when are some ‘stuff’ automatically garbage collected, or does that never happen? Do we need to go through each variables, functions and connections to delete them?

I’ve moved this to Scripting Support since it’s not a creation. Please check which category you’re posting in.

The garbage collector collects objects/values from scripts that are no longer in use/needed by the script. For example, when you destroy an instance and remove any references to it, it gets picked up by the garbage collector. This happens automatically in Lua but in other lower-level programming languages like C, there’s no built-in garbage collector and you’re tasked with managing memory yourself.

2 Likes

Lua uses a garbage collector that runs from time to time to collect dead objects when they are no longer accessible from the Lua program(script). All objects including tables, userdata, functions, thread, string and so on are subject to automatic memory management.

So basically when you set any tables, userdata, functions, thread, strings to nil or if you run Destroy() on an instance, it will be automatically garbage collected. Events on the other hand need to be manually disconnected when you’re done using them.

1 Like

Garbage collecting is the concept of freeing up memory that is no longer in use. For example if you store something in a variable and then lose all references to the stored object (by leaving the scope, etc) that object is still stored in memory so the garbage collector goes through and frees its position in memory. In general, you really don’t need to worry about garbage collecting at all. But if it really does bother you, just references to things you won’t use anymore and disconnect events.

You don’t have to worry about GC, on Roblox we don’t have any control over it.

Worry about other things that would be best for your interest.

And don’t forget to use the search bar next time…


2 Likes

Can you explain which events? like you mean remote events? if yes then you mean any remote event ever in whole game I should a Disconnect() to it?