collectgarbage() - when should you use it in your Fuse?

User avatar
SecondMan
Site Admin
Posts: 3338
Joined: Thu Jul 31, 2014 5:31 pm
Answers: 1
Location: Vancouver, Canada
Been thanked: 63 times
Contact:

collectgarbage() - when should you use it in your Fuse?

#1

Post by SecondMan » Mon Nov 03, 2014 10:13 pm

I just read this tutorial: http://luatut.com/collectgarbage.html about the collectgarbage() function, after seeing it used in the Frame Average Fuse and I have a question about it.

When should you use it?

For example, in order to make the XSheet script work in my Time Machine Fuse, I am creating quite a few tables over and over again, at every character typed in the XSheet text field. That results in a lot of intermediate tables that are not used in the final output. Will the memory used for those be cleaned up or should I add the above function? And where would be the best place to add it? Would one be enough at the end of a function or is it worth doing it multiple times in different function parts? Like in every loop that creates tables?

Could it make a Fuse more stable because there are less leftover pointers in memory? Or is the Lua compiler/Fusion "smart" enough to not have to worry about it?

User avatar
PeterLoveday
Fusioneer
Posts: 140
Joined: Sun Sep 14, 2014 6:09 pm
Answers: 5
Been thanked: 10 times

Re: collectgarbage() - when should you use it in your Fuse?

#2

Post by PeterLoveday » Tue Nov 04, 2014 12:23 am

Generally speaking, I wouldn't worry about it unless you area dealing with many large objects.

In the case of FrameAverage, it collects many images at once, so ensuring that they get flushed makes sense - they may total hundreds of MB or more.

The other case is where you want to *force* all references to something to be cleaned up; such as a script that might want to close a comp, it can set the variable it is remembering it in to nil, but it might not immediately lose the reference, so a collectgarbage() in this case can force that.

So, if you have a lot of memory in use and much of it can be freed up, or if you want to force a resource to be allowed to close, then by all means use it. But if all you're dealing with is some tables, I wouldn't worry about it.

- Peter

User avatar
SecondMan
Site Admin
Posts: 3338
Joined: Thu Jul 31, 2014 5:31 pm
Answers: 1
Location: Vancouver, Canada
Been thanked: 63 times
Contact:

Re: collectgarbage() - when should you use it in your Fuse?

#3

Post by SecondMan » Tue Nov 04, 2014 10:50 am

OK gotcha - thank you, that's very helpful.

I might need it soon for my next endeavour. :mrgreen: