__GC (Mind blowing)

Warning: This code can blow your mind

It’s basicly some code to detect whenever something garbage collects.
You can connect a handler to it, a function to be called when it happens.
(You won’t have access to the obj, though, since it’s already GC-ed)

I call it genius, and I think I can easily claim that I already blew some minds in #roblox :smiley:
(Happy my, happy me, my egotism is rising to a peak, and I like it :smiley: :smiley: :D)

local a,b = {},setmetatable({},{__mode="v"})
local function RegisterGC(obj,func)
	local key = tick()
	a[func],b[key] = key,obj
end
local function HandleGC()
	for k,v in pairs(a) do
		if not b[v] then
			a[k],b[v] = nil k()
		end
	end
end

local test = true
local obj = {}
RegisterGC(obj,function() test = false print("GC-ed") end)
obj = nil
while test do
	print("Waiting for GC...")
	wait(0.1) HandleGC()
end
1 Like

I have no idea what this means, but I’ll take interest anyway. So, since I have no idea what this means - and well I could maybe use it or not. Could you explain this to me like I’m some form of four year old?

Hm. Kinda disappointed, it sounded like you were able to listen for the disconnect with metatable magic. Nope, just checking if the object has been collected yet in a busy loop. :frowning:

You know how garbage collection works in Lua?

You have for example a table full with data, and that table is stored in _G.LOL and a local ‘DOH’.
Once you set _G.LOL to nil, and DOH to nil (or the functions that got access to them are GC-ed),
that object is valid to be garbage collected.
It’s literally garbage: Nothing can access it (in Lua), and it’s just wasting RAM.
Every now and then (a few times per second I think) Lua (well, the C-side) collects garbage.
It goes over all objects, checks if they are still accessible, and if they aren’t, removes them from memory.
(Imagine if you have a function that requires a temporary local table:
Everytime you use the function, that table gets stored in memory, taking up RAM.
Due garbage collecting, it gets removed from RAM once it isn’t used anymore)

It’s very difficult to detect GC normally, because when an object is GC-ed, it’s already gone.

(My explanation could be horrible to understand, check the Lua wikis maybe?)

1 Like

Yeah, this is more like a substitute for __gc, but it seems like a working one.
(You don’t have access to the object itself, because it’s already GC-ed, but still)

Another example of garbage collection happens when concatenating two strings together.

local s = “hey!\n”
s = s…“my name is roblox”
print(s)

When you concatenate a string, lua creates a new string where it sort of copies the new concatenated strings together. Then the code moves on, and in the background, lua garbage collects the old string that was used for concatenation because it is no longer being used

[quote] This really has no application on ROBLOX.
And off of ROBLOX, Lua has the metamethod __gc, which you’ve just re-implemented in a very costly way. The purpose of __gc is for memory management and in this case, you really haven’t helped that. [/quote]

Some people have a need for __gc (surprised me too, but some people actually do)

And it’s just nice to write some very haxy stuff now and then.

You cannot detect garbagecollection without interfering with garbagecollection. The garbage is always equally likely to be or not to be collected.

There are metatables that can look at data without preventing it from being collected.
This is useful if you’re dealing with heavy amounts of data on your server and want to modify output and processing based on how much is being used.

A practical application for this could be seen in Territory Conquest. If there’s a lot of units pathfinding, you may want to know how many paths are actually there to determine if you should sacrifice memory usage for processing usage or vice versa, saving a lot of lag that we currently see in Territory Conquest.

Huh? Why does wiki mention __gc, if roblox doesn’t let you use it? It says nothing about it being disabled.

hmm… Schrödinger and Heisenberg did not account for weak tables…

So, you’re just putting it into a weak-value table and waiting for the value to disappear?

Doesn’t seem to have it on the metatable page, but it does on the metamethod page.
http://wiki.roblox.com/index.php?title=Metamethods#Notes

the breeze tickled my mind