Jump to content

What does garbagecollect do?


JeViCo

Recommended Posts

  • Moderators

The collectgarbage cleans objects. The object you are probably familiar with is the table(a powerful hybrid between an object and an array if you know JavaScript).

variable = {}

 

When you create a table, it is not just creating it and save it in a variable. < This is actually incorrect.

This is incorrect because you are not saving the table inside of a variable.

 

A table is a THING, unlike the values 72675467 or "random string". THINGS are of course saved inside of the memory just like variables, but they exist not at the same place.

Which means that what is actually is saved inside of a variable is a reference TO that TABLE.

 

The benefit of a reference instead of just a value:

local a = {1}

local b = a
b[2] = 10000

local c = a
c[3] = "IIYAMA"

iprint(a) -- {1, 10000, "IIYAMA"}

-- magic!

a = nil
iprint(a) -- nil
iprint(b) -- {1, 10000, "IIYAMA"}
iprint(c) -- {1, 10000, "IIYAMA"}

-- magic!

b[4] = "this is more trash" 

b = nil 
iprint(a) -- nil
iprint(b) -- nil
iprint(c) -- {1, 10000, "IIYAMA", "this is more trash" }

-- magic!

c = nil

iprint(a) -- nil
iprint(b) -- nil
iprint(c) -- nil

 

As it isn't always known when all references are destroyed(which means the table can't be accessed any more), the garbagecollector will clean it after a while. (which happens in cycles)

Do not use that function, if this is still hard to understand. This LUA function kills performance if not used correctly. = (The collector is forced to make a full cycle)

So better not use it at all, as LUA can do a fine job doing it itself.

 

 

 

 

 

 

 

 

 

 

Edited by IIYAMA
  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...