Garbage Collection. Any good reference sources?

Monkey Forums/Monkey Programming/Garbage Collection. Any good reference sources?

jondecker76(Posted 2014) [#1]
I'm evaluating porting some existing code over to Monkey (specifically the cpptool target)

I can't seem to find any definitive reference to the garbage collection system.

- Can the GC be called manually? I don't see any documented functions, but I've seen mention of gc_mark and gc_sweep in some posts on here.

- I see that the cpptool target does have some directives for different GC modes, including turning the garbage collector off. If the GC is turned off, how can I free memory manually? Is there any good reference for these directives?

- Is there any way of adding desctuctors to objects?

Thanks


ziggy(Posted 2014) [#2]
Can the GC be called manually? I don't see any documented functions, but I've seen mention of gc_mark and gc_sweep in some posts on here.

As far as I know, you can't, but I don't see why you would ever need this

I see that the cpptool target does have some directives for different GC modes, including turning the garbage collector off. If the GC is turned off, how can I free memory manually? Is there any good reference for these directives?

If it's turned off, memory can't be collected, it'll be free when program ends. That's an option for quick tools that do not allocate lots of memory, so they can perform a bit quicker, but on real world apps, it makes no sense to turn it off.

Is there any way of adding desctuctors to objects?

No, destructors are not supported on Monkey.


jondecker76(Posted 2014) [#3]
Found the CPP_GC_MODE and CPP_GC_TRIGGER documentation here: http://www.monkey-x.com/docs/html/Programming_App%20config%20settings.html

Still, some questiosn remain.
- What exactly is mode 1? Documentation states that it's called every "OnWhatever". What does this mean exactly? Doe this mean it's called after "OnCreate", "OnUpdate" and "OnRender"? If so, wouldn't this mean that it only works in conjunction with the App (or derrived App) class?
- Same question for mode 2. What exactly does "incremental collect every GC allocation" mean? What constitutes a GC allocation?
- CPP_GC_TRIGGER.. Does this affect both mode 1 and mode 2? Essentially, is this saying that resources aren't freed until 8MB of no-longer-needed memory is pooled up?


ziggy(Posted 2014) [#4]
Ah yes, unless you're using Mojo, I would recommend you to set it to incremental every GC allocation. (mode 2) so you can be sure memory is collected properly.


jondecker76(Posted 2014) [#5]
Thanks for chiming in ziggy

I guess now all I need to figure out is if CPP_GC_TRIGGER is even valid in mode 2, and figure out what constitutes a GC allocation (I would assume any time an object is created, though I would like to know for sure). It would also be interesting to know how mode 2 affects the undocumented bl.thread module. I may have to dig through the source to find out.

Here is another interesting link that shows manual garbage collection by extending the stdcpp target:
http://monkeycoder.co.nz/Community/posts.php?topic=5286

EDIT:
Updating this with my findings peeking around the source real quick for my own notes (though others may find it helpful as well).
-In lang.cpp, line 251, gc_obj_alloc is called. It clearly shows that in mode 2, that the CPP_GC_TRIGGER is what triggers gc_collect_all() to be called. It also answers some of my original questions. Mainly, that each time an object is created a gc_object is allocated. During this process, if memory allocation > CPP_GC_TRIGGER then the GC kicks in and frees up memory. Memory won't be freed again until TRIGGER is exceeded again.
- Also in lang.cpp, if you look, the CPP_GC_MAX_LOCALS, it's just a simple stack/reference counter and basically if you have more than 8192 locals allocated, anything beyond this isn't managed by the garbage collector (from what I'm seeing anyways). So therefore, CPP_GC_MAX_LOCALS on it's own doesn't do anything to trigger garbage collection
- It would seem to me that brl.thread may actually be safe to use while in mode 2. Interesting...


ziggy(Posted 2014) [#6]
Yes, a GC allocation happens when a new object is created. It's very easy to test, just create a sample application that generates large amounts of garbage, and see if it gets collected:
'#CPP_GC_MODE=2  
Const ITERATIONS:= 10000
Function Main()
	For Local i:Int = 0 To ITERATIONS
		Local list:= New List<String>
		For Local j:Int = 0 To ITERATIONS
			list.AddLast(String.FromChars([
			Int(Rnd(0, 65535)),
			Int(Rnd(0, 65535)),
			Int(Rnd(0, 65535))]))
		Next
	Next
End