Question on garbage collection

Monkey Forums/Monkey Beginners/Question on garbage collection

MonoDesire(Posted 2015) [#1]
Hi all,

I am confused about how objects are garbage collected or not. Here is a code example:

Strict

Class Coordinates
  Public
  
  Method New()
    x = -1
    y = -1
  End Method
  
  Field x:Int, y:Int
End Class

Class Test
  Public
  
  Method GetCoordinates1:Coordinates()
    Local coordinates:Coordinates = New Coordinates()
    coordinates.x = 10
    coordinates.y = 20    
    Return coordinates
  End Method
  
  Method GetCoordinates2:Int(coordinates:Coordinates)
    coordinates.x = 100
    coordinates.y = 200
    Return 0
  End Method
  
End Class

Function Main:Int()
  Local test:Test = New Test()
  
  ' Test #1
  Local coordinates1:Coordinates = test.GetCoordinates1()
  Print("x=" + coordinates1.x + "; y=" + coordinates1.y)
  
  ' Test #2
  Local coordinates2:Coordinates = New Coordinates()
  test.GetCoordinates2(coordinates2)
  Print("x=" + coordinates2.x + "; y=" + coordinates2.y)
  
  Return 0
End Function


The printout from the above program is the following:

x=10; y=20
x=100; y=200

In Test #2 I guess that a reference is passed for the Coordinate object, so that should be okay.

In Test #1 I thought that such operation would have been ilegal, but it seems to work (printout is as expected). Is it that when "test.GetCoordinates1()" is called (in Main), the actual object is copied into "Local coordinates1:Coordinates"? Because if it's just the reference, I guess it would have been garbage collected when GetCoordinates1() was done executing... or?

Any input is appreciated!

Thanks! :-)


ImmutableOctet(SKNG)(Posted 2015) [#2]
Okay, so Monkey is fully garbage collected. There's no copy operations; at least, language defined operations you should worry about. When 'GetCoordinates1' is executed, it creates an object with the garbage collector. What's returned is a reference to this object. Optimization aside, your local 'coordinates' variable has a reference to this new object. Of course, from here, you mutate what's in the object as expected. Then, when you're finished, you return the reference to the garbage collected object. This can then be used by whatever code called the function or method. In your case, you passed the reference to 'coordinates1'. This means the object stays around. Monkey has several garbage collection modes, and several targeted collectors, depending on the target. The main idea of garbage collection is that, you create an object, and you have automatic references to that object. When all references are gone, the object is free. When the object is free, it's up to the collector to eventually deal with it. This can range wildly between GC implementations. For games, Monkey mainly uses a deterministic approach, where a set amount of time each update can be spent managing memory. Despite the differences in garbage collection modes, the idea remains the same: Don't care about how the object gets freed. That doesn't mean you shouldn't care about the costs of creating objects, just that you shouldn't care when they get destroyed.

So, following this mindset, creating an object, then passing a reference as a return-value is perfectly acceptable.

EDIT: If you're interested in learning more about memory management in Monkey, I've made a pretty good number of posts about it. Here's one I made about the C++ targets' GC, and this thread has some posts (Several by myself) about memory management in general. You could also tread through my other forum posts, but I don't recommend it.


Gerry Quinn(Posted 2015) [#3]
If either of those were illegal, nobody would use garbage collection. The idea of GC is that the object only gets destroyed when there is no reasonable way for you to refer to it again.

When GetCoordinates1() is called, a new Coordinates object is created, and a reference to it (essentially, its address in RAM) is returned. While any function or class has a record of that reference, the object will not be destroyed.

If main() were an ordinary function, all the objects created would be eligible for garbage collection after it returns. If it stored any of them in a global variable or otherwise left a reachable reference, the objects would stay around until any references are gone.


MonoDesire(Posted 2015) [#4]
Wow, thank you so much both for your very comprehensive explanations! :-)

Thank you for spending time on this. Hope someone else finds those replies as useful as I just did.

I see now how the garbage collection works (on this high level, which is good enough for at this point in time).

Again, thanks!


Gerry Quinn(Posted 2015) [#5]
The main thing about GC is that you really don't have to worry about it making your objects disappear unexpectedly. It won't do that. So long as you remember where an object you created ought to be, it will still be there!


MonoDesire(Posted 2015) [#6]
@Gerry Quinn: I have played around for some hours now, and the way that one in the Monkey does not have to think about GC at all, is really really convenient! :-)