Native Code and Monkey's Garbage Collection

Monkey Archive Forums/Monkey Tutorials/Native Code and Monkey's Garbage Collection

AdamRedwoods(Posted 2012) [#1]
This is a loose set of notes, and is current for Monkey V66 (and 67).
Some of these notes may be incorrect, use at your own risk.

---------------

Preface:
Most modern languages tend to have garbage collection (GC) built-in, so this is really only needed in C++.
Also be aware that this GC does not work with multiple threads at this time.


Here's how the GC works:
It's a mark-sweep algorithm, called every cycle (about once a frame).
Say you have an object, and you want monkey to manage the object's GC, then you will need to extend the object's class with Monkey's native built-in Object Class (ie: class MyClass : public Object {} ....etc). Any data types within the class are managed, as are classes using "new" (that extend Object), but not dynamically allocated memory (malloc).

wxWidgets example:
class wxMonkeyTimer :  public Object, public wxTimer {
public:
	wxMonkeyTimer() {};
	~wxMonkeyTimer() {};
	void mark() { 
		Object::mark();
	};

};


By extending from the base monkey class "Object", the class's "new" is overloaded and added to the GC's main mark_roots function, which is called once per update cycle.

You really only need "Object::mark()" if you want to extend that class with another one, in monkey or natively, and have it managed as well.

I also discovered order is important for extending the base Object, it should be first before other extensions. (c++ experts probably already know this)

If you are allocating your own memory in your class, then you need to use gc_mark or gc_mark_q. I *think* the difference between the two is that gc_mark is managed memory collection, gc_mark_q is unmanaged, you collect it in your own class. ( i need to test and confirm this. )
You will also need to gc_assign() your managed variables upon creation since you are not using the constructor.


AdamRedwoods(Posted 2012) [#2]
Good question was brought up:
do I need to gc_mark pointers?

No, the class treats pointers as an address type so it is kept locally in the class, which is managed. I think it's ULONG on 64-bit (correct me if im wrong).

So if the pointer points to a memory block, monkey's GC won't see the memory, only the pointer. Therefore, your class will need to malloc() and free() it manually before releasing the pointer.


AdamRedwoods(Posted 2012) [#3]
Good question was brought up:
do I need to gc_mark pointers?

No, the class treats pointers as an address type so it is kept locally in the class, which is managed. I think it's ULONG on 64-bit (correct me if im wrong).

So if the pointer points to a memory block, monkey's GC won't see the memory, only the pointer. Therefore, your class will need to malloc() and free() it manually before releasing the pointer.


AdamRedwoods(Posted 2013) [#4]
Monkey's Garbage Collection info (V71) has been updated here:
https://github.com/blitz-research/monkey/wiki/Garbage-collection