Skip to content

Garbage Collection

TheRealMichaelWang edited this page May 6, 2021 · 2 revisions

Garbage Collection

FastCode uses a combination of reference counting and tracing in it's garbage collection algorithm. Because of FastCode's unique garbage collection system, a good understanding of its general functionality is key to writing effective FastCode programs.

Reference Apartments

FastCode garbage collector store's values (numericals, characters, etc...) in an unique reference apartment. When a reference is requested, a pointer to it's reference apartment is returned so that there are never two instances of any reference. Each reference apartment has a inner count of every possible access route - for example:

a = 8

set's a's reference apartment's value to 8 while maintaining a inner counter of 1 because there is one way to access it - through a. When the variable a falls out of context, a's reference apartment's inner counter would be adjusted accordingly.

Keeping the previous example in mind, when you run

b = ref a

a's reference apartment's inner counter is incremented by one because there is one more way to access a's reference apartment - through b. It's important to note that there is no concept of ownership within the said reference apartment. Neither a or b own that reference apartment - they merely share it. When either a or b fall out of context, their reference apartments inner counter is decremented accordingly.

Arrays and structures (non-primitive types) have children references. Keep in mind that non-primitives never delete their children references - tracing comes in when a reference apartment's inner count must adjust it's children references. The same procedures apply for each child reference, therby "tracing" the entire variable-tree. Here is an example that demonstrates the above:

array = [1, 2, 3, 4, 5]

when the collection, array is declared and allocated, a total of 6 reference apartments are created within the garbage collector. One reference apartment is created for array, and 5 are created for array[0], array[1], ..., and array[5].

myref = ref array

When the above is run, array and myref's reference apartment's counter is incremented to 2. The same is applies to that of all the children apartments.

array = null

Hypothetically, if the above is run, every child reference apartments' counter is going to be decremented by 2 - the value of the parent apartment's references.

Garbage Collection

Every single primitive and non-primitive value declared get's stored in a linked-list like, global data structure within the garbage collector. That is why garbage collector is passed into built-in functions: because you need the runtime's only garbage collector to allocate valid reference apartments.

Within the garbage collector, the linear reference data structure is split into certain range's, which will be referred to as garbage frames. When a garbage collection cycle is called, the value deletion/de-allocation is only applied to the apartments within the most-recently-added garbage frame. Depending on whether the runtime's trying to wrap up a call frame, or only want's to perform some in-function garbage collection, the most-recent garbage collection frame may be popped, and removed. The garbage-frames are stored in a stack.

When the garbage collector checks the reference apartments within the current garbage frame, reference apartments (and the values within) that have a counter value of 0 are deleted.

Note that because of the decentralized reference-counting/tracing system, garbage collector sweeps can be called as frequently as needed without adding any significant computational cost. This will keep the memory overhead much lower.

Clone this wiki locally