Jump to content

garbage collector


IIYAMA

Recommended Posts

  • Moderators

Hi,

I was curious when the garbage collector would clean something like this:

function testFunction ()
  local testVariable = 100
  local function firstFunctionInBlock ()
  	print("first function, testVariable: " .. testVariable )
  	testVariable = testVariable + 1
  end
  local function secondFunctionInBlock ()
  	print("second function, testVariable: " .. testVariable)
 	testVariable = testVariable + 1
  end
  return function () firstFunctionInBlock() secondFunctionInBlock ()  end
end

 

function testFunction ()
--------------------------
-- function block --

--------------------------
end

 

Executed with:

local newCreatedFunction = testFunction()
newCreatedFunction()
newCreatedFunction()
newCreatedFunction()
--
print("--")
--
local newCreatedFunction2 = testFunction()
newCreatedFunction2()
newCreatedFunction2()
newCreatedFunction2()

 

Results:

first function, testVariable: 100
second function, testVariable: 101
first function, testVariable: 102
second function, testVariable: 103
first function, testVariable: 104
second function, testVariable: 105
--
first function, testVariable: 100
second function, testVariable: 101
first function, testVariable: 102
second function, testVariable: 103
first function, testVariable: 104
second function, testVariable: 105

 


 

I assume when I nil the function that is active in the block.

newCreatedFunction = nil

 

It will clear the first created function block by the garbage collector.
 


 

newCreatedFunction2 = nil

 

Same goes for clearing the second created function block.

 

 

I did like to hear your opinion about this matter! :P

 

  • Like 3
Link to comment

Nice! That's actually really interesting. For some reason, I was hoping testVariable might go out of scope when testFunction returns but then saw the results you posted and it was kinda surprising to me that it was not garbage-collected and remained in memory stack otherwise firstFunctionInBlock and secondFunctionInBlock would have failed to increment it.

  • Like 1
Link to comment
  • Moderators

 

It is indeed a kinda unexpected/ yet expected that testVariable remains part of the scope per function.

 

But the main reason why I posted this, is:

function testFunction ()
  local testVariable = 100
  local function firstFunctionInBlock ()
      print("first function, testVariable: " .. testVariable )
      testVariable = testVariable + 1
  end
  local function secondFunctionInBlock ()
      print("second function, testVariable: " .. testVariable)
     testVariable = testVariable + 1
  end
  return function () firstFunctionInBlock() secondFunctionInBlock ()  end
end

This new created function is created inside the function block and yet it is the reason why this block remains to exist.

 


 

If this function is destroyed it should automatic clear the whole function block with the garbage collector:

Which has been created when the testFunction is called:

local newCreatedFunction = testFunction()

function testFunction ()
  local testVariable = 100
  local function firstFunctionInBlock ()
      print("first function, testVariable: " .. testVariable )
      testVariable = testVariable + 1
  end
  local function secondFunctionInBlock ()
      print("second function, testVariable: " .. testVariable)
     testVariable = testVariable + 1
  end
  return function () firstFunctionInBlock() secondFunctionInBlock ()  end

end

 

 

  • Like 1
Link to comment
  • Moderators

no, I am not expecting that.

I am expecting it to create a new copy of the default state of the block every time the function has been called. Which is lua actually doing at the moment.

It is just strange that when the function (that has been created in testVariable) is destroyed, the block(copy) where it has been created also gets destroyed.

 

I know it is complicated. :(

 

 

 

Link to comment

Right. A simple visual representation after the testFunction returns:

local newCreatedFunction = function () -- or local function newCreatedFunction ()
    -- testVariable, firstFunctionInBlock, secondFunctionInBlock still exist here for newCreatedFunction only!
    firstFunctionInBlock() 
    secondFunctionInBlock () 
end 

 

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...