Just checking about Lists and Garbage Collection

Monkey Forums/Monkey Beginners/Just checking about Lists and Garbage Collection

Grey Alien(Posted 2014) [#1]
I've been reading up on the GC just to check my understanding of it is correct (it seems to be pretty much the same as BlitzMax), but I'd like to 100% verify something please:

If I have a list of objects and I call myList.Clear() it will null all the pointers to the objects in the list and also null any pointers in the objects (that might point to other objects for example) right?

I don't have to manually call my own Delete() method on each object in the list in order to null pointers in the object, right?

Same thing for arrays if I get a full array and just go myArray = null or myArray = [] right?

What about if one of the objects contained an image or sound? In that case I would call Discard() on the image/sound myself as the GC won't clear it, right (or will it)? Currently I store all my images and sounds in a separate bank and just reference them as needed so it's not an issue, but I'd still like to make sure of the correct method.

I also gather there are two GC modes that operate either after every OnUpdate or every object allocation. Which is best of iOS/Android please? Thx

Many thanks in advance for any advice.


Aman(Posted 2014) [#2]
Garbage collection is handled really well in monkey. You should not worry about it. Whenever something goes out of scope, it gets collected.

myList.Clear() will null all the pointers to the objects in the list but it will not null any pointers these objects have. This is handled by GC which will also handle out of scope objects resulted by your myList.Clear() as well.

However, what you said about arrays is correct.Images and Sounds are also handled by GC. But I would not recommend having images and sounds to go out of scope if they are going to use them again as they take longer to allocate memory.


Grey Alien(Posted 2014) [#3]
Thanks for the reply. OK so nulling an array is safe too then, good to know.

I only let images/sounds go out of scope when a level or screen isn't needed any more and can be loaded in at a later time.


zoqfotpik(Posted 2014) [#4]
One thing I just noticed for the first time when reading through the docs the other day was the Pool collection type which is basically a statically allocated list, avoiding the GC.


Grey Alien(Posted 2014) [#5]
Yeah I made a particle pool as that is the most commonly allocated/deallocated type of object in my games.